在shell脚本里怎么判断某命令存不存在

2020-11-20  本文已影响0人  风吹路过的云

在shell中怎么判断某命令存不存在?一开始想着用which,但发现不好使。。。正确的使用方式,应如下:

hash go 2> /dev/null || { echo >&2 "go sdk没有安装..."; exit 1; }

在网上查到的,例子:

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

参考:
https://www.cnblogs.com/embedded-linux/p/6206064.html
原文链接:
https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script/677212#677212

上一篇下一篇

猜你喜欢

热点阅读