shell中如何判断某一命令是否存在
参考:
http://www.cnblogs.com/tuzkee/p/3755230.html
https://segmentfault.com/q/1010000000156870
http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
避免使用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; }
If your hash bang is /bin/sh
then you should care about what POSIX says. type
and hash
's exit codes aren't terribly well defined by POSIX, and hash
is seen to exit successfully when the command doesn't exist (haven't seen this with type
yet). command
's exit status is well defined by POSIX, so that one is probably the safest to use.
If your script uses bash
though, POSIX rules don't really matter anymore and both type
and hash
become perfectly safe to use. type
now has a -P
to search just the PATH
and hash
has the side-effect that the command's location will be hashed (for faster lookup next time you use it), which is usually a good thing since you probably check for its existence in order to actually use it.
As a simple example, here's a function that runs gdate
if it exists, otherwise date
:
gnudate() {
if hash gdate 2>/dev/null; then
gdate "$@"
else
date "$@"
fi
}
In summary:
Where bash
is your shell/hashbang, consistently use hash
(for commands) or type
(to consider built-ins & keywords).
When writing a POSIX script, use command -v
.
首先要说明的是,不要使用which来进行判断,理由如下:
1、which非SHELL的内置命令,用起来比内置命令的开销大,并且非内置命令会依赖平台的实现,不同平台的实现可能不同。
# type type
type is a shell builtin
# type command
command is a shell builtin
# type which
which is hashed (/usr/bin/which)
2、很多系统的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

3、许多系统的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; }
犀利的原文,可以在这里查看:
shell中如何判断某一命令是否存在的更多相关文章
- shell 中如何判断前一个命令是否执行成功
shell 中如何判断前一个命令是否执行成功 通过判断返回值来解决: if [ $? -eq 0 ];then 命令正确的分支 else 命令失败的分支 fi
- shell中条件判断if中的-z到-d的意思
shell中条件判断if中的-z到-d的意思 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 ...
- 在shell中如何判断字符串是否为有效的IP地址【转】
转自 在shell中如何判断字符串是否为有效的IP地址_echoisecho_新浪博客http://blog.sina.com.cn/s/blog_53a844e50100xxus.html 近来需要 ...
- shell中条件判断if中的-z到-d
shell中条件判断if中的-z到-d的意思 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真.[ -c FILE ] 如果 ...
- Shell中比较判断
一.shell判断数组中是否包含某个元素:ary=(1 2 3)a=2if [[ "${ary[@]}" =~ "$a" ]] ; then echo & ...
- shell 中的判断
一.if的基本语法: if [ command ];then 符合该条件执行的语句 elif [ command ];then 符合该条件执行的语句 else 符合该条件执行的语句 ...
- shell 中的 eval 及 crontab 命令
eval eval会对后面的命令进行两遍扫描,如果第一遍扫描后,命令是个普通命令,则执行此命令:如果命令中含有变量的间接引用,则保证间接引用的语义.也就是说,eval命令将会首先扫描命令行进行所有的置 ...
- shell中条件判断语法与判断条件小结
1. IF条件判断语法: if Athen dosthelif B dosthelse dosthfi 2. 判断条件: 2.1 字符串判断 str1 = str2 当两个串有相同 ...
- 【转】shell中如何判断一个变量是否为空
判断一个脚本中的变量是否为空,我写了一个这样的shell脚本: #!/bin/sh #filename: test.sh para1= if [ ! -n $para1 ]; then echo &q ...
随机推荐
- 块级&行内(内联)元素
行内元素列表 <a>标签可定义锚 <abbr>表示一个缩写形式 <acronym>定义只取首字母缩写 <b>字体加粗 <bdo>可覆盖默认的 ...
- ci获取当前url链接的分组,控制器,方法
public function role() { $ci= &get_instance();//ci控制器超级对象,就是把所有执行的方法打印出来 $directory = substr($ci ...
- NV Maxwell architecture
按照NVIDIA的路线图来看,GTX 600以及GTX 700系列所采用的Kepler架构已经垂垂老矣,最早在明年第一季度,其继任者Maxwell架构可能就会和我们正式见面了.目前外媒已经放出了关于M ...
- 一些App的User-Agent
天猫 Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; MI 2C Build/KTU84P) AppleWebKit/537.36 (KHTML, like ...
- php 使用GD库上传图片以及创建缩略图
php 使用GD库上传图片以及创建缩略图 GD库是PHP进行图象操作一个很强大的库. 先在php.ini里增加一行引用:extension=php_gd2.dll 重启apache.做一个测试页 ...
- LENGTH() CHAR_LENGTH()
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_length LENGTH(str) Returns the ...
- 21335592 ROWS
CREATE TABLE w_big SELECT * FROM ( SEELCT * FROM w_tab UNION ALL SELECT * FROM w_tab_copy_modify ) ...
- Java中的匿名对象
匿名对象就是没有明确给出名字的对象.一般匿名对象只使用一次,而且匿名对象只在堆内存中开辟空间,而不存在栈内存的引用. 一个普通的常量字符串就可以表示一个匿名String对象. 比如可以 int len ...
- 关于FireMonkey TGrid赋值的一点小研究
FireMoneky的TStringGrid用法和VCL里面的差不多, 但是另一个TGrid实在是奇葩, 几乎找不到给单元格赋值的方法(除了使用LiveBind) 看了其源码, 发现只要给某个Colu ...
- sqlserver 计算 百分比
,),))+'%' As 百分比 NUMERIC(P,S) P的默认值是:38 S的默认值是:-84~127 numeric(a,b)函数有两个参数,前面一个为总的位数,后面一个参数是小数点后的位数, ...