Mandar Pande
Mandar Pande

Reputation: 12984

"echo $?" works in linux/aix only, not in solaris/hp-ux [strange ! or i'm missing any thing ]?

if i fired "zcat1" [it's not exists anywhere] on all the platforms and immediately does echo "$?", I'm getting failure value [1] in Linux and AIX only not in HP-UX and Solaris ? Please let me know, what is missing, I'm using sh shell everywhere ? Do i need to do any settings ?

uname -a
Linux xxxxx 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
[root@xxxxx tmp_a]# which zcat1
/usr/bin/which: no zcat1 in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/bin/admincmd/:/root/bin)
[root@cllin11 tmp_a]# echo $?
1

uname -a
AIX xxxxxx 1 7 00CE6F1F4C00
[root@xxxxx /]# which zcat1
which: 0652-141 There is no zcat1 in /usr/bin /etc /usr/sbin /usr/ucb /usr/bin/X11 /sbin /usr/java5/jre/bin /usr/java5/bin /root/bin /opt/VRTSvcs/bin /opt/VRTSvxfs/cfs/bin /etc/vx/bin /opt/pware/bin /opt/VRTSvcs/bin /etc/vx/bin /opt/VRTSvxfs/cfs/bin /opt/VRTS/bin /opt/pware/bin /opt/VRTSat/bin /opt/VRTSat/bin.
[root@claix01 /]# echo $?
1

# uname -a
HP-UX xxx B.11.23 U 9000/800 3547050632 unlimited-user license
# which zcat1
no zcat1 in /usr/sbin /usr/bin /usr/ccs/bin /usr/contrib/bin /usr/contrib/Q4/bin /opt/perl/bin /opt/ipf/bin /opt/hparray/bin /opt/nettladm/bin /opt/fcms/bin /opt/ssh/bin /opt/mx/bin /opt/wbem/bin /opt/wbem/sbin /opt/resmon/bin /usr/bin/X11 /opt/sec_mgmt/bastille/bin /opt/drd/bin /opt/dsau/bin /opt/dsau/sbin /opt/firefox /opt/gnome/bin /opt/perf/bin /opt/ignite/bin /usr/contrib/kwdb/bin /opt/mozilla /var/opt/netscape/servers/shared/bin /var/opt/netscape/servers/bin /var/opt/netscape/servers/bin/slapd/server /var/opt/netscape/server7/shared/bin /var/opt/netscape/server7/bin /opt/graphics/common/bin /opt/perl_32/bin /opt/perl_64/bin /opt/prm/bin /usr/sbin/diag/contrib /opt/sfm/bin /opt/sec_mgmt/spc/bin /opt/java1.4/jre/bin /opt/spb/bin /opt/swa/bin /opt/hpsmh/bin /opt/thunderbird /opt/upgrade/bin /opt/gwlm/bin /usr/contrib/bin/X11 /sbin /home/root
# echo $?
0

uname -a
SunOS xxxxxx 5.10 Generic_141445-09 i86pc i386 i86pc
# which zcat1
no zcat1 in /usr/sbin /usr/bin /opt/VRTS/bin /var/VRTS/bin/
# echo $?
0

Upvotes: 2

Views: 992

Answers (3)

jordanm
jordanm

Reputation: 34974

The other answers already cover why you can not rely on the exit status of which. In bash, you should use the builtin type, which will not change depending on OS.

$ type -P ls
/bin/ls
$ echo $?
0
$ type -P askdjas
$ echo $?
1

Upvotes: 0

mata
mata

Reputation: 69062

well, the hp-ux manpages for which don't say anything about an exit status, so you shouldn't really expect one.

as alternative you could use type, which is part of POSIX and should be required to return an error code on failure.

Upvotes: 1

ennuikiller
ennuikiller

Reputation: 46965

You are staring into the abyss of POSIX implementation hell ..... here is a tidbit from the "Oracle at Redwood City" to help you along:

None shall pass the POSIX river without permission

Upvotes: 2

Related Questions