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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to designate block of code

Status
Not open for further replies.

redsss

Programmer
Mar 17, 2004
72
US
I need the unix shell equivalent to the "perl" die command

e.g. die "you must be root user to run this so goodbye"

I would expect this to work but it doesnt
Code:
[ "$UID" = "0" ] || (echo your not root so goodbye; exit -1 )
echo if you see this then you must be root

Is there a way to group the 2 commands as a block without making an actual "if" block?
 
[ "$UID" = "0" ] || { echo your not root so goodbye; exit -1; }
Use curly braces instead of parentheses.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I had tried that unsuccessfully, but in addition I now see that you have to have a space after the opening brace and a semicolon and space before the closing brace. thanks!
 
You might save yourself some typing by encapsulating that in a function:
Code:
function die
{
    [ "$1" ] && echo $1 >&2
    exit 1
}

[ "$UID" = "0 ] || die "You're not root."


The reason your original attempt didn't work was because commands envlosed in parentheses are run in a subshell, and the exit command caused the subshell to exit, but not the top level one running the rest of your script.


By the way, I second the grammar correction. It's annoying enough to read that mistake in general; it's extra annoying when a program says it to you.

You should take extra care in writing a program's output; you don't want every person who actually knows English to find your program annoying and unprofessional.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top