bash $X++ sintax
Chris F.A. Johnson
cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org
Thu May 25 20:21:25 UTC 2006
On Thu, 25 May 2006, Lennart Sorensen wrote:
> On Thu, May 25, 2006 at 03:25:54PM -0400, Neil Watson wrote:
>> That is the equivalent to ++ only for strings. Example:
[snip]
>> # Generate passwords for all users
>> apg -m 10 -M CN -n ${NUM} | {
>>
>> while read PW; do
>>
>> X=$(( $X + 1 ))
>>
>> # Username will be USER+NUM e.g. sftp1
>> USERNAME=${USER}${X}
>>
>> # Set password for user
>> echo $PW | passwd -u $USERNAME --stdin
>>
>> # Log change for later email
>> MSG="${MSG} User: $USERNAME Password; $PW"
>>
>> done
>> }
>>
>> echo $MSG #| mail -s "SSH Transfer Password Updates" $RECIP
>> exit 0
>>
>> MSG is as expect until is finishes the code block. After that it is reset
>> to
>> its original state. It's like MSG is scoped differently inside the code
>> block.
>> I am not aware that bash is scope conscious.
>
> If you want it global, you have to export it. Change the original place
> you made MSG to have export in front, then it will be global and the
> block should change the global rather than the local copy of it.
>
> like: export MSG="....
No, that will not help. The elements of a pipeline are executed in
a subshell, and the parent shell cannot see any changes made.
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group. Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml
More information about the Legacy
mailing list