我们在写shell脚本时,有时候需要根据某个命令是否存在来决定是否执行某些操作,可以有两种方法来判断命令是否存在,参考代码如下:
判断linux命令是否存在,方法1:
#!/bin/bash
if which mycmd >/dev/null 2>&1; then
echo "mycmd exists"
else
echo "mycmd not exists"
fi
判断linux命令是否存在,方法2:
#!/bin/bash
if ! command -v mycmd >/dev/null 2>&1; then
echo "mycmd not exists"
else
echo "mycmd exists"
fi
评论区