bash question

Sjoerd Mullender sjoerd at acm.org
Wed Oct 31 13:58:21 UTC 2007


Miner, Jonathan W (CSC) (US SSA) wrote:
> 
> 
> -----Original Message-----
> From:	fedora-list-bounces at redhat.com on behalf of Dario Lesca
> Sent:	Wed 10/31/2007 09:12 AM
> To:	Fedora Project List
> Cc:	
> Subject:	bash question
> 
> In this shell script:
> 
>> #!/bin/bash
>> i=0
>>
>> printf "a a a\nb b b\nc c c\n" |
>> while read x
>> do
>>         i=$[i+1]
>>         echo "$x: ($i)"
>> done
>>
>> echo $i
> 
> -------------------------------
> 
> By pipe'ing the commands together, you cause the while loop to be executed in a subshell. Change your script to be:
> 
> #!/bin/bash
> i=0
> 
> while read x
> do
>          i=$[i+1]
>          echo "$x: ($i)"
> done << _EOF_
> a a a
> b b b
> c c c
> _EOF_
> 
> echo $i
> 
> 
> 

Or:

#!/bin/bash
printf "a a a\nb b b\nc c c\n" | {
i=0
while read x
do
    i=$[i+1]
    echo "$x: ($i)"
done
echo $i
}



-- 
Sjoerd Mullender

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 370 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20071031/b3f94e4f/attachment-0001.sig>


More information about the fedora-list mailing list