How to catch /bin/bash: bad interpreter error

recently came across an issue when running a bash script executed in a csh shell. This was outputed: /bin/bash: bad interpreter: No such file or directory. The problem was bash was not on the environment path. After adding bash, this was fixed. I want to make sure that in the future, if this ever happened again for some reason, I can handle this. I am wonder what exit code this is? or is this just printed out on stderr? I want to catch this and fail the main script. Any ideas on how to handle this?

I have this segment:

bash sc142.sh ##################################################################### # Check for processing errors ##################################################################### if ($status != 0) then exit (-1) endif 
16

2 Answers

I tried this on Debian, the exit status for a bad interpreter error is 126. So you can do:

/path/to/scriptname arg ... if ( $status == 126 ) then echo "scriptname failed" exit 1 endif 

Note that a false positive is possible. If the last command in the script you're running exits with status 126, you won't be able to tell the difference.

11

The exit code will be non-zero. The exact exit code depends on the environment. You may get 127 (command not found) but you may also get another non-zero exit code in certain shells.

In your csh script you can set the -e option which will cause the script to exit immediately if any commands fail.

#!/bin/csh -e false echo not printed 
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like