Thursday, August 2, 2007

csh - executing a unix cmd and checking for its exit status

Everyone knows how to check the exit status of a command invoked via csh/tcsh

%> cat mycmd
#!/bin/csh -f
exit 0;

%> cat wrapper.csh
#!/bin/csh -f

mycmd
if ($status) then
echo "error executing mycmd"
exit 1
endif
echo "successful execution of mycmd"

%>


But there is another easier way if you don't care about the actual value of the exit status.

%> cat easier_wrapper.csh
#!/bin/csh -f

if ! { mycmd } then
echo "error executing mycmd"
exit 1
endif
echo "successful execution of mycmd"

%>


Notice the use of curly braces around mycmd.

2 comments:

Anonymous said...

Even after 4 years I leave this simple tip handy :)

Unknown said...

The entrance strategy is actually more important than the exit strategy. See the link below for more info.


#exit
www.ufgop.org