shell 用command 命令

from:https://blog.csdn.net/u011068702/article/details/80787824

command [-pVv] command [arg ...]
Run command with args suppressing the normal shell function  lookup.  Only  builtin commands  or  commands  found in the PATH are executed.  

If the -p option is given, the search for command is performed using a default value for PATH that is  guaranteed  to find all of the standard utilities.  

If either the -V or -v option is supplied, a description of command is printed.  The -v option  causes  a  single  word indicating  the  command or filename used to invoke command to be displayed; the -V option produces a more verbose description.  
If the -V or -v  option  is  supplied, the exit status is 0 if command was found, and 1 if not.  If neither option is supplied and an error occurred or command cannot be found, the  exit  status  is  127.

Otherwise, the exit status of the command builtin is the exit status of 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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章