Re: bash while read question

2010-05-05 Thread CyberLeo Kitsana
On 05/05/2010 08:25 PM, Evuraan wrote:
> I cant figure out why the variable in in loop2 does not hike to +1?
> (its a friday, i am dazed, I admit. but this should not be a mystery!)
> any help would be much appreciated.
> 
> $ cat loop2
> #! /bin/bash
> 
> date > /tmp/somefile
> b="1"
> cat /tmp/somefile | while read blah; do
> let b=(b+1)
> done
> echo "variable is $b"

This particular syntax executes the 'while' block in a subshell. The
variables set or altered in the subshell are never propagated back up to
the parent shell.

-- 
Fuzzy love,
-CyberLeo
Technical Administrator
CyberLeo.Net Webhosting
http://www.CyberLeo.Net


Furry Peace! - http://.fur.com/peace/
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


RE: bash while read question

2010-05-05 Thread Evuraan
2010/5/5 CyberLeo Kitsana :
> On 05/05/2010 08:25 PM, Evuraan wrote:
>> I cant figure out why the variable in in loop2 does not hike to +1?
>> (its a friday, i am dazed, I admit. but this should not be a mystery!)
>> any help would be much appreciated.
>> 
>> $ cat loop2
>> #! /bin/bash
>>
>> date > /tmp/somefile
>> b="1"
>> cat /tmp/somefile | while read blah; do
>> let b=(b+1)
>> done
>> echo "variable is $b"
>
> This particular syntax executes the 'while' block in a subshell. The
> variables set or altered in the subshell are never propagated back up to
> the parent shell.
>
duh, i get it now, anytime stuff is piped , a subshell is evoked:

http://tldp.org/LDP/abs/html/subshells.html says,


Redirecting I/O to a subshell uses the "|" pipe operator, as in ls -al
| (command).


thanks for the "reset"..!

> --
> Fuzzy love,
> -CyberLeo

>
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"