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

 更多请见:http://www.mark-to-win.com/tutorial/51830.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值