bash $X++ sintax

Lennart Sorensen lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Thu May 25 19:53:24 UTC 2006


On Thu, May 25, 2006 at 03:25:54PM -0400, Neil Watson wrote:
> That is the equivalent to ++ only for strings.  Example:
> $X = "foo";
> $Y = "bar";
> 
> $word = $X;
> $word .= $Y;
> 
> print $word
> 
> foobar
> 
> I think I have it figured out but I'm still missing something.  Consider
> this script:
> 
> #!/bin/bash
> 
> # User prefix
> USER="sftp"
> 
> # Number or users
> NUM=5
> 
> # Email report recipient
> RECIP="nhwatson-CDzewpS4IfcWRvO7F5PPEFaTQe2KTcn/@public.gmane.org"
> 
> # Mail messages
> MSG="Transfer passwords for today (`date`):"
> 
> # 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="....

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