Linux系统检查命令是否存在

in with 0 comment  访问: 2,705 次

兼容POSIX的command

如果要兼容POSIX的话可以使用command

command -v <command>

示例如下:

if ! [ -x "$(command -v curl)" ]; then
  echo 'Error: curl is not installed.' >&2
  exit 1
fi

使用Bash的bash或type

bash环境可以使用hashtype命令

hash <command> 
type <command> 

检查wget是否存在,示例如下:

check_wget() {
    if hash wget 2>/dev/null; then
        wget "$@"
    else
        curl "$@"
    fi
}
WeZan