2

如果我在所有平台上触发“zcat1”[它在任何地方都不存在],并立即回显“$?”,我在 Linux 和 AIX 中得到失败值 [1],而在 HP-UX 和 Solaris 中没有?请让我知道,缺少什么,我到处都在使用 sh shell?我需要做任何设置吗?

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
4

3 回答 3

2

您正盯着 POSIX 实施地狱的深渊.....这里有一个来自“红木城的 Oracle”的花絮,可以帮助您:

未经许可,任何人不得通过 POSIX 河

于 2012-05-13T14:46:34.940 回答
1

好吧,hp-ux 联机帮助页which没有说明退出状态,因此您不应该真的期待退出状态。

作为替代方案,您可以使用POSIXtype一部分,并且应该要求在失败时返回错误代码。

于 2012-05-13T14:48:58.043 回答
0

其他答案已经涵盖了为什么您不能依赖which. 在 bash 中,您应该使用 builtin type,它不会因操作系统而改变。

$ type -P ls
/bin/ls
$ echo $?
0
$ type -P askdjas
$ echo $?
1
于 2012-05-13T15:29:28.080 回答