Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Curly braces in bash allow script to see variable?

Status
Not open for further replies.

jouell

MIS
Nov 19, 2002
304
US
Code:
#!/bin/bash
set -u

ls /etc

{

ls /

SUBJECT=TEST

} | mail -s "$SUBJECT" $ME

This returns "unbound variable" or just a blank subject w/o set -u. I thought the commands/script outside of the curly braces can see the variable $SUBJECT as it's supposed to be global.

I am missing something basic here.

Thanks!
jouell

 
Hi

jouell said:
I thought the commands/script outside of the curly braces can see the variable $SUBJECT as it's supposed to be global.
Yes, just a simple pair of braces ( {} ) does nothing special :
Code:
[blue]master #[/blue] { foo=bar; }; echo "~$foo~"
~bar~
But if the brace delimited block's output is piped, the block will be executed in a sub-process and nothing set there will be ever visible to the outside :
Code:
[blue]master #[/blue] { foo=bar; } | echo "~$foo~"
~~
The solution is to use process substitution instead of piping :
Code:
[blue]master #[/blue] { foo=bar; } > >( echo "~$foo~" )
~bar~
Process substitutions is available in Bash and Ksh, but not in the original Bourne shell.


Feherke.
 
I wonder if I spoke too soon on this, doing as suggested on the cmd line is OK, but as a shell script seems not working?


Code:
OK:

# { foo=bar; } > >( echo "~$foo~" )
# ~bar~

Code:
# cat i
#!/bin/bash
{ foo=bar; } > >( echo "~$foo~" )

Neither of these work:

#bash i
~~

#./i
~~

# bash -x  i
+ foo=bar
++ echo '~~'
~~
 
Hi

Hmm... There must be some issue with the expansion order. The variable survives and is available outside the braces, but not in the substituted process. Strange, the documentation mentions nothing about such restriction.

By the way, do you really need to set that variable inside the braces ?


Feherke.
 
Hi Feherke

>>By the way, do you really need to set that variable inside the braces ?

Not a must, I can work around it.

What I'm really trying to do is this:

{

comand1
...
comandX

if something good; then

SUBJECT=OK

else

SUBJECT=NOTOK

fi

} mail -s $SUBJECT


I could redirect out to a file and send it separately w/the subject but I'd really like to make this clean and efficient.

jouell
 
Hi

Unless that "something good" means running commands who's output has to be also included in the mail, I would prefer tho move the [tt]if[/tt] statement out from the brace block.

Sorry, tried some combinations, but none seems to work from a script.


Feherke.
 
Hi Feherke


Right those commands need to be included in the email. I can rewrite the script w/a temp file.

Thanks for looking here!
jouell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top