bash $X++ sintax

Chris F.A. Johnson cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org
Thu May 25 20:30:37 UTC 2006


On Thu, 25 May 2006, Neil Watson wrote:
[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.

    Each element of a pipeline is executed in a separate subshell, and
    the parent shell knows nothing of what goes on behind those closed
    doors.

    Move the "echo $MSG ..." inside the braces and all will be well.

-- 
    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