bash 运行脚本时找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21855298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:36:20  来源:igfitidea点击:

commands not found when running script

bashshellgrepcommandpid

提问by TheRedSeth

I have a very basic script that keeps spitting back that the commands are not found. Ive looked all over this site and can not find an answer that works for me. The path to bash is correct. Ive checked the script with od. Ive run dos2unix. None of this helps me.

我有一个非常基本的脚本,它不断吐槽找不到命令。我已经浏览了这个网站,但找不到适合我的答案。bash 的路径是正确的。我已经用 od 检查了脚本。我已经运行了 dos2unix。这些都没有帮助我。

SCRIPT:

脚本:

    #!/bin/bash
    HYBRISPROC=`ps -eo pid,command | grep [h]ybris | grep -v grep | awk '{print }'`
    echo "Looking for Hybris..."
    echo $HYBRISPROC

RESULTS:

结果:

    ./HybrisStopStart.sh: line 5: ps: command not found
    ./HybrisStopStart.sh: line 5: grep: command not found
    ./HybrisStopStart.sh: line 5: awk: command not found
    ./HybrisStopStart.sh: line 5: grep: command not found
    Looking for Hybris...

Any ideas? If I run the command just on its own it works fine. Ive tried it as sudo as well and et the same results.

有任何想法吗?如果我单独运行该命令,它可以正常工作。我也试过它作为 sudo 并得到相同的结果。

TIA

TIA

采纳答案by MLSC

How about it?

这个怎么样?

#!/bin/bash
HYBRISPROC=`ps -eo pid,command | grep [h]ybris | grep -v grep | awk '{print }'`
echo "Looking for Hybris..."
echo "$HYBRISPROC"

(OR)

(或者)

#!/bin/bash
HYBRISPROC="ps -eo pid,command | grep [h]ybris | grep -v grep | awk '{print }'"
echo "Looking for Hybris..."
bash -c "$HYBRISPROC"

(OR)

(或者)

#!/bin/bash
HYBRISPROC="ps -eo pid,command | grep [h]ybris | grep -v grep | awk '{print }'"
echo "Looking for Hybris..."
eval "$HYBRISPROC"

TOTALY:

总计:

you can see the difference:

你可以看到不同之处:

#!/bin/bash
LS=`ls -l`
echo $LS    #nasty way
echo 
echo "$LS"  #good way

回答by vadchen

Try to add

尝试添加

PATH="$PATH:/usr/bin:/bin"

PATH="$PATH:/usr/bin:/bin"

before code. Looks like bin directory is not on your path. So the commands are not found.

在代码之前。看起来 bin 目录不在您的路径上。所以找不到命令。