Search code examples
pythoncshexit-code

Exit csh script based on previous error code


I have a shell script running a python script. In the event of a non-zero exit code I would like the exit the current csh script. The example code below in the csh script does not seem to be working.

./pythonfile.py
if $? != 0 then
    echo 'Something went wrong!' $?
    exit 1
endif

Solution

  • Most csh versions are actually tcsh. In tcsh, both $? and $status should work. Looking at the Fixes file, it seems this has been the case since 1992...

    The problem in your code, is that the if statement will override the value of the $status/$? variables; you need to use an intermediate variable to store the exit code.

    false
    set code = $?
    if $code != 0 then
        echo 'Something went wrong! ' $code
        exit 1
    endif