找出一个命令是否包含在环境变量路径的文件夹中

时间:2022-07-28 19:23:29

I cannot find out how to see if a command is included in folders of environment variable PATH. I tried the command:

我找不到如何查看一个命令是否包含在环境变量路径文件夹中。我试着命令:

$type -t $command 

but it doesn't work.

但它不工作。

Can anyone help me?

谁能帮我吗?

2 个解决方案

#1


2  

This should work:

这应该工作:

if [[ $(type -p command) ]]; then
echo "Found"
else
echo "Not Found"
fi

You can use -t too (See exceptions at bottom.).

您也可以使用-t(见下面的异常)。

Or (only testing the exit status with type):

或(仅用类型测试退出状态):

if type command >& /dev/null; then 
echo "Found"
else
echo "Not Found"
fi

Note: See exceptions at bottom.

注意:请参见下面的异常。

Another solution (using hash):

另一个解决方案(使用散列):

if [[ ! $(hash command 2>&1) ]]; then
echo "Found"
else
echo "Not Found"
fi

Note: See exceptions at bottom.

注意:请参见下面的异常。

Exceptions:

例外:

type command
type help
hash command
hash help
type -t command
type -t help

command and help are bash built-ins, they are not in any path in PATH environment variable. So the other methods except the first one (with -p option) will Print out Found for bash built-in commands which are not in any path in environment PATH variable.

命令和帮助是bash内置的,它们不在路径环境变量中的任何路径中。因此,除了第一个方法(带有-p选项)之外的其他方法将输出bash内置命令,这些命令在环境路径变量的任何路径中都没有。

Better use the first method (with -p option) if you only want to check if it's located in the paths in PATH environment variable.

如果您只想检查它是否位于PATH环境变量中的路径,最好使用第一个方法(带有-p选项)。

Or if you want to use type -t then change the if statement like this:

或者如果你想使用-t类型,那么改变if语句如下:

if [[ $(type -t command) == file ]]; then

#2


0  

Do you mean looking at your path? Similar to:

你是说看着你的路吗?类似于:

$ set | grep PATH

Oh, now I understand. Checking for an executable in the path is fairly easy. I usually use something like the following:

哦,现在我明白了。检查路径中的可执行文件相当容易。我通常使用如下内容:

## test for exe in PATH or exit
exevar="$(which exe 2>/dev/null)"
[ x = x$exevar ] && { echo "'exe' not in path"; exit 1; }

## exe in path, continue
echo "exevar = $exevar"

or use type -p to eliminate the call to which

或者使用-p类型消除对which的调用

## test for exe in PATH or exit
exevar="$(type -p exe 2>/dev/null)"
[ x = x$exevar ] && { echo "'exe' not in path"; exit 1; }

## exe in path, continue
echo "exevar = $exevar"

#1


2  

This should work:

这应该工作:

if [[ $(type -p command) ]]; then
echo "Found"
else
echo "Not Found"
fi

You can use -t too (See exceptions at bottom.).

您也可以使用-t(见下面的异常)。

Or (only testing the exit status with type):

或(仅用类型测试退出状态):

if type command >& /dev/null; then 
echo "Found"
else
echo "Not Found"
fi

Note: See exceptions at bottom.

注意:请参见下面的异常。

Another solution (using hash):

另一个解决方案(使用散列):

if [[ ! $(hash command 2>&1) ]]; then
echo "Found"
else
echo "Not Found"
fi

Note: See exceptions at bottom.

注意:请参见下面的异常。

Exceptions:

例外:

type command
type help
hash command
hash help
type -t command
type -t help

command and help are bash built-ins, they are not in any path in PATH environment variable. So the other methods except the first one (with -p option) will Print out Found for bash built-in commands which are not in any path in environment PATH variable.

命令和帮助是bash内置的,它们不在路径环境变量中的任何路径中。因此,除了第一个方法(带有-p选项)之外的其他方法将输出bash内置命令,这些命令在环境路径变量的任何路径中都没有。

Better use the first method (with -p option) if you only want to check if it's located in the paths in PATH environment variable.

如果您只想检查它是否位于PATH环境变量中的路径,最好使用第一个方法(带有-p选项)。

Or if you want to use type -t then change the if statement like this:

或者如果你想使用-t类型,那么改变if语句如下:

if [[ $(type -t command) == file ]]; then

#2


0  

Do you mean looking at your path? Similar to:

你是说看着你的路吗?类似于:

$ set | grep PATH

Oh, now I understand. Checking for an executable in the path is fairly easy. I usually use something like the following:

哦,现在我明白了。检查路径中的可执行文件相当容易。我通常使用如下内容:

## test for exe in PATH or exit
exevar="$(which exe 2>/dev/null)"
[ x = x$exevar ] && { echo "'exe' not in path"; exit 1; }

## exe in path, continue
echo "exevar = $exevar"

or use type -p to eliminate the call to which

或者使用-p类型消除对which的调用

## test for exe in PATH or exit
exevar="$(type -p exe 2>/dev/null)"
[ x = x$exevar ] && { echo "'exe' not in path"; exit 1; }

## exe in path, continue
echo "exevar = $exevar"