雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

linux的Shell脚本中如何判断某个命令是否存在

2024-09-08 14

我们在写shell脚本时,有时候需要根据某个命令是否存在来决定是否执行某些操作,可以有两种方法来判断命令是否存在,参考代码如下:

判断linux命令是否存在,方法1:

#!/bin/bash
if which mycmd /dev/null 21; then
    echo mycmd exists
else
    echo mycmd not exists
fi

判断linux命令是否存在,方法2:

#!/bin/bash
if ! command -v mycmd /dev/null 21; then
    echo mycmd not exists
else
    echo mycmd exists
fi
更新于:8天前
赞一波!

文章评论

全部评论