Bash Centos7“哪个”命令

asg*_*ard 8 bash command yum which centos7

我意识到这可能是一个愚蠢的问题,但是我安装了Centos-7最小服务器,并且“ which”命令不存在或丢失。我有一个需要它的脚本,但是我找不到要安装它的yum软件包。代码在下面,来自make文件。

which grep > /dev/null 2> /dev/null

if test "$?" != "0"
then
    echo "\"grep\" command not found."
    echo "Installation is aborted."
    exit 1
fi
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激...这是困难的,即使不是不可能的谷歌

Win*_*ute 11

要在CentOS中查找软件包,请使用 yum whatprovides

yum whatprovides *bin/which
Run Code Online (Sandbox Code Playgroud)

在这种情况下,该包称为which,因此

yum install which
Run Code Online (Sandbox Code Playgroud)

应该把它拉进去。


Sri*_*uru 6

which 您可以使用type命令代替命令。

type grep > /dev/null 2> /dev/null
if test "$?" != "0"
then
    echo "\"grep\" command not found."
    echo "Installation is aborted."
    exit 1
fi
Run Code Online (Sandbox Code Playgroud)

  • 而 `type -P grep` 输出到 `grep` 的路径,正是 `which` 的作用。 (3认同)
  • 这是更好的解决方案。“which”是不可移植的,而“type”是 POSIX 强制要求的并内置于 shell 中。 (2认同)