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
![](https://common.cnblogs.com/images/copycode.gif)
# 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
![](https://common.cnblogs.com/images/copycode.gif)
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 ...
随机推荐
- gridview header增加排序图标
/* add sorting icons to gridview sort links */a.asc:after, a.desc:after { position: relative; top: 1 ...
- Scrum会议3
组名称:天天向上 项目名称:连连看 参会成员:王森(Master)张金生 张政 栾骄阳 时间:2016.10.18 已完成内容: 1.GUI布局设计 2.通过在网上大量阅览代码,大体了解连连看游戏制作 ...
- POJ 3254 Corn Fields(状压DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13732 Accepted: 7216 Desc ...
- CSS3 Transform——transform-origin
2012年9月,W3C组织发布了CSS3变形工作草案.CSS3变形允许CSS把元素转变为2D 或3D空间,这个草案包括了CSS3 2D变形和CSS3 3D变形. CSS3变形是一些效果的集合,比如平移 ...
- MySQL 日期和时间戳互相转换
① 时间戳转换成日期 FROM_UNIXTIME 例如: 数据表中 invest_time 存储的是时间戳,如 1429063399 使用 FROM_UNIXTIME 可以把时间戳转换为日期: sel ...
- NSDate 获取明天、后天的日期
NSDate * senddate=[NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIde ...
- UIbutton 圆角和边线
#define WhiteColor [UIColor whiteColor]#define YellowColor Color_RGB(253,131,42,1)//主题黄#define Font ...
- Rails--%w用法[转]
%Q 用于替代双引号的字符串. 当你需要在字符串里放入很多引号时候, 可以直接用下面方法而不需要在引号前逐个添加反斜杠 (\") >> %Q(Joe said: "Fr ...
- 11号了,还有三天上线-改bug
+(NSDictionary *)replacedKeyFromPropertyName { return @{ @"doctorId": @"id" }; ...
- Introducing the Accelerated Mobile Pages Project, for a faster, open mobile web
https://googleblog.blogspot.com/2015/10/introducing-accelerated-mobile-pages.html October 7, 2015 Sm ...