Jose Ortiz
Jose Ortiz

Reputation: 311

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

Upvotes: 0

Views: 1053

Answers (2)

Barmar
Barmar

Reputation: 782099

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.

Upvotes: 1

jspcal
jspcal

Reputation: 51914

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

Upvotes: 1

Related Questions