打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
shell之用command在终端判断是否存在这个命令

1、command解释

command命令在shell脚本里面,如果发现有个函数和我们需要执行的命令同名,我们可以用command用来强制执行后面的命令,而不是同名函数,然后我们也可以在shell脚本里面判断莫个命令是否存在,我们平时一般用which命令也行。

2、测试代码

#!/bin/bash

function pwd()
{
    echo "I am pwd function"
}

echo "shell run pwd"
pwd

echo "shell command pwd"
command pwd

if  command -v pwd > /dev/null; then
    echo "pwd command has found"
else
    echo "pwd command has not found"
fi

if  command -v pwd1 > /dev/null; then
    echo "pwd1 command has found"
else
    echo "pwd1 command has not found"
fi

3、运行结果

./command 
shell run pwd
I am pwd function
shell command pwd
/home/chenyu/Desktop/linux/dabian/python
pwd command has found
pwd1 command has not found

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
第7章 shell程序设计
shell 十三問之第三问:別人 echo、你也 echo ,是問 echo 知多少
shell 基础 $(cd `dirname $0`;pwd)
shell命令
我使用过的Linux命令之type
linux bash shell 最常用的函数和指令(备忘查询)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服