找出POSIX系统上是否存在命令 [英] Find out if a command exists on POSIX system

查看:125
本文介绍了找出POSIX系统上是否存在命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过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.

此外,我最近发现which命令本身不是POSIX(请参见

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆