如何在 Linux 中检查命令是否存在
Posted on Wed, 25 Dec 2024 13:55:53 +0800 by LiangMingJian
如何在 Linux 中检查命令是否存在
实现
在 Linux 中,你可以使用command -v来检查特定命令是否存在,在 Bash 脚本中使用以下代码检查。
if ! command -v <the_command> &> /dev/null
then
echo "<the_command> could not be found"
exit
fi
在一些特别的环境中,还可以使用hash,type来进行检查。
hash <the_command> # For regular commands. Or...
type <the_command> # To check built-ins and keywords
另外,请避免使用which。在许多操作系统中which不会设置退出状态,其不会返回值。意味着如果if which foo不会返回foo不存在,总会报告foo存在。此外,which还会将输出更改或将结果挂载在包管理器中。因此请尽量避免使用which,请改用以下方法将结果输出到空2>/dev/null,以避免程序出错。
$ 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; }
一个简单的函数示例如下,如果命令存在则运行,否则返回 gdate date
gnudate() {
if hash gdate 2>/dev/null; then
gdate "$@"
else
date "$@"
fi
}