Abhishek Goswami Abhishek Goswami - 6 months ago 21
Bash Question

Determine command status in C shell

I wrote my c shell script which need to determine the command status. In the below program

line 4
command was not successful. But I got status still
0
. I do not understand why.
As per my understanding, I should get
1
. Am I right?
Line No 3
command was successful.
I know it is very basic question but it is not homework. We need to determine the job status
So I have tried with simple script

Line 4
give me error that it is unable to load this version tool

1 #!/bin/csh -f
2 source /global/etc/csh.cshrc
3 module unload her
4 module load her/2012
5 echo $status
6 if ( $status != 0) then
7 echo "Error: abhishek Unable to execute module load her/2012"
8 exit
9 endif

Answer

The problem is you are testing the status after the echo command. The echo command was successful, so it reset the status to 0. Here is some sample code to show the issue

#!/bin/csh -f

sh -c "exit 2"
echo status is $status or $?
echo status is $status or $?

which outputs

status is 2 or 2
status is 0 or 0

Another thing you can do is use #!/bin/csh -fe which causes the script to exit as soon as the error occurs.