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

Explanation

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

相关文章推荐

(二)NS3如何编译、运行脚本和 Command Line命令行参数设置

二、编译和运行脚本 7. 编译和运行脚本主要步骤 1) 将编写的脚本复制到ns-3.22/scratch目录下(可以在ubuntu窗口界面直接复制) 进入ns3目录: /ns-3.22 $ cp ex...
  • E_pen
  • E_pen
  • 2015-04-28 16:44
  • 3245

NS3 Command Line Arguments 使用命令行参数改变脚本

转自:http://blog.sina.com.cn/s/blog_7ec2ab360102wxgr.html 除了通过NS_LOG环境变量外,另一个不用编译就可以改变NS3脚本的方...

Linux Command Line and....ch11(构建基本脚本)

本章内容: 使用多个命令 创建脚本文件 显示消息 使用变量 输入输出重定向 管道 数学运算 退出脚本 11.1 使用多个命令shell脚本的关键在于输入多个命令并处理每个命令的结果,甚至需要将一个命令...

Linux Command Line and....ch16(控制脚本)

本章内容: 处理信号 以后台模式运行脚本 禁止挂起 作业控制 修改脚本优先级 脚本执行自动化 16.1 处理信号 16.1.1 重温Linux信号 bash shell会处理收到的1和2信号=...

linux下执行脚本,提示Command not found解决办法

遇上了这个坑,搞了好久没搞定,参考了前辈的文章,终于搞好了,分享如下 原文地址:http://www.cnblogs.com/GordonWorld/archive/2012/03/19/240...

Linux系统shell脚本判断变量文件目录:权限、是否存在、空值、相等

1、字符串判断 str1 = str2      当两个串有相同内容、长度时为真 str1 != str2      当串str1和str2不等时为真 -n str1        当串的长...

Linux shell脚本中判断变量文件目录:权限、是否存在、空值、相等

#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数判断$myPath是否存在并且...

Linux系统shell脚本判断变量文件目录:权限、是否存在、空值、相等

先写一些基本语法: 1、字符串判断 str1 = str2      当两个串有相同内容、长度时为真 str1 != str2      当串str1和str2不等时为真 -n str...
  • hpwzd
  • hpwzd
  • 2012-08-30 11:45
  • 1414

Command-linux命令

  • 2015-01-06 20:57
  • 1.52MB
  • 下载

Linux系统shell脚本判断变量文件目录:权限、是否存在、空值、相等

先写一些基本语法: 1、字符串判断 str1 = str2      当两个串有相同内容、长度时为真 str1 != str2      当串str1和str2不等时为真 -n str...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)