Operating System - Linux
1748262 Members
3609 Online
108760 Solutions
New Discussion юеВ

command "not found" exit code different when in an interactive shell

 
Daavid Turnbull
Frequent Advisor

command "not found" exit code different when in an interactive shell

I am trying to find a clean and simple way of determining if a command was "not found" (or if it was found having a non zero exit code).

Interactively I get:
__________________

> CheckEnvironment.sh
sh: CheckEnvironment.sh: not found
> echo $?
127
__________________


But performing similar logic from within a script I get (with set -x on):
___script________

CheckEnvironment.sh $0 $File
if [ $? -ne 0 ]
___output________
+ CheckEnvironment.sh ./start_RMS_services.sh.in /tmp/start_RMS_services.sh.in.1028238.errors
/tmp/start_RMS_services.sh.in.1028238.sh[23]: CheckEnvironment.sh: not found
+ [ 0 -ne 0 ]
__________________

So how can I tell if a command is "not found" within a script?

(Note: I am hoping to whereis or which etc.)
Behold the turtle for he makes not progress unless he pokes his head out.
5 REPLIES 5
Daavid Turnbull
Frequent Advisor

Re: command "not found" exit code different when in an interactive shell

Note should read:
(Note: I am hoping to avoid using whereis or which etc.)
Behold the turtle for he makes not progress unless he pokes his head out.
Steven Schweda
Honored Contributor

Re: command "not found" exit code different when in an interactive shell

Which interactive shell?
Which shell for the script?
Daavid Turnbull
Frequent Advisor

Re: command "not found" exit code different when in an interactive shell

Doh!!

I worked it out.

My issue was that I was creating the script as an inline file and had neglected to escape the $0... which was of course 0 at the point when the script was being produced... so this code always passed the test:
________
CheckEnvironment.sh ./start_RMS_services.sh.in /tmp/start_RMS_services.sh.in.938148.errors
if [ 0 -ne 0 ]
then
________

I shall close this thread now to avoid embarrassing replies ;-)


Behold the turtle for he makes not progress unless he pokes his head out.
Daavid Turnbull
Frequent Advisor

Re: command "not found" exit code different when in an interactive shell

See comments above
Behold the turtle for he makes not progress unless he pokes his head out.
Dennis Handly
Acclaimed Contributor

Re: command "not found" exit code different when in an interactive shell

For a real shell, there is a whence builtin. It returns no output but exit status of 1.

You may have to use "set +e" to turn off errexit when you do the whence in non-iteractive shells.