找出POSIX系统上是否存在命令 [英] Find out if a command exists on POSIX system
问题描述
我希望能够通过shell脚本判断命令是否存在于任何POSIX系统上.
I want to be able to tell if a command exists on any POSIX system from a shell script.
在Linux上,我可以执行以下操作:
On Linux, I can do the following:
if which <command>; then
...snip...
fi
但是,当命令不存在时,Solaris和MacOS which
不会给出退出失败代码,它们只会向STDOUT打印一条错误消息.
However, Solaris and MacOS which
do not give an exit failure code when the command does not exist, they just print an error message to STDOUT.
Also, I recently discovered that the which
command itself is not POSIX (see http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html)
有什么想法吗?
推荐答案
command -v
是POSIX指定的命令,其作用是什么.
command -v
is a POSIX specified command that does what which does.
定义为在找不到命令或发生错误时返回> 0.
It is defined to to return >0 when the command is not found or an error occurs.
这篇关于找出POSIX系统上是否存在命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!