1、很多系统的which并不设置退出时的返回值,即使要查找的命令不存在,which也返回0。
-
# which ls
-
/usr/bin/ls
-
# echo $?
-
0
-
# which aaa
-
no aaa in /usr/bin /bin /usr/sbin /sbin /usr/local/bin /usr/local/bin /usr/local/sbin /usr/ccs/bin /usr/openwin/bin /usr/dt/bin
-
# echo $?
-
0
2、
许多系统的which实现,都偷偷摸摸干了一些“不足为外人道也”的事情。所以,不要用which,可以使用下面的方法:
-
$ command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
-
$ type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
-
$ hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
阅读(1271) | 评论(0) | 转发(0) |