linux脚本中判断命令是否存在 hash <the_command>

转载 2017年10月07日 01:31:02

POSIX compatible:

command -v <the_command>

For bash specific environments:

hash <the_command> # For regular commands. Or...
type <the_command> # To check built-ins and keywords


Avoid which. Not only is it an external process you’re launching for doing very little (meaning builtins like hash, type or command are way cheaper), you can also rely on the builtins to actually do what you want, while the effects of external commands can easily vary from system to system.

Why care?

  • Many operating systems have a which that doesn’t even set an exit status, meaning the if which foo won’t even work there and will always report that foo exists, even if it doesn’t (note that some POSIX shells appear to do this for hash too).
  • Many operating systems make which do custom and evil stuff like change the output or even hook into the package manager.

So, don’t use which. Instead use one of these:

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

(Minor side-note: some will suggest 2>&- is the same 2>/dev/null but shorter – this is untrue. 2>&- closes FD 2 which causes an error in the program when it tries to write to stderr, which is very different from successfully writing to it and discarding the output (and dangerous!))

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 "$@"
        date "$@"

linux bash如何判断脚本命令参数是否存在

linux bash处理较简单的参数。如./ 10而很少使用./test -n 10这种带选项的方式。 典型用法为:#!/bin/bashif [ x$1 != x ] then ...
  • u011729865
  • u011729865
  • 2017年09月02日 09:49
  • 239


dig(key, ...) → objectclick to toggle source Extracts the nested value specified by the seque...
  • shiralwz
  • shiralwz
  • 2017年01月22日 11:05
  • 564


今天有个朋友问我如何判断一个设备节点是否存在,觉的很简单的问题,结果破费周折。 那个朋友首先发过来一个脚本说不行: #!/bin/sh echo "Find usb disk......." ...
  • qq160816
  • qq160816
  • 2016年06月12日 11:48
  • 1816

shell实例 ----判断输入的用户名是否存在与/etc/passwd中

#!/bin/bash if [ $# -le 0 ]; then   echo "Please input the right arg" else   i=`cat /etc/passwd ...
  • Phoenix_99
  • Phoenix_99
  • 2012年02月10日 10:12
  • 1781


判断目录是否存在shell 判断语句 流程控制 “if” 表达式 如果条件为真则执行then后面的部分: if ….; then…. elif ….; then …. else …. fi大多数情况...
  • luoaijun12
  • luoaijun12
  • 2016年11月20日 12:04
  • 771


-b FILE FILE exists and is block special -c FILE FILE exists and ...
  • huoyin
  • huoyin
  • 2016年05月29日 22:07
  • 1146


hadoop判断文件是否存在   在shell中判断一个HDFS目录/文件是否存在   直接看shell代码:   hadoop fs -test -e /hdfs_dir if [ $...
  • zengxiaosen
  • zengxiaosen
  • 2016年10月27日 16:08
  • 1804


举例说明如下:   $(hide) for GENERATED_MODULE_FILE in `find $(ATH_ANDROID_SRC_BASE)/compat/ \        ...
  • jianzhengzhouzjz
  • jianzhengzhouzjz
  • 2012年11月13日 09:22
  • 1133


s=123 print(s) #del s if('s' in vars()): print(s) else: print('sdf')
  • moliyiran
  • moliyiran
  • 2017年08月09日 21:32
  • 670


1:查找文件夹 如果文件夹存在,则打印一句存在,否则打印不存在 这里的话可以自由加一些指令。 if [ test -d 文件夹名称 ] ; then echo "文件夹存在!" else echo "...
  • morixinguan
  • morixinguan
  • 2016年02月25日 12:30
  • 8426
您举报文章:linux脚本中判断命令是否存在 hash <the_command>