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 hashbecome 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; }

犀利的原文,可以在這里查看:

http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script/677212#677212


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM