bash 在bash脚本中出现“找不到命令”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5642521/
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-09 20:24:14  来源:igfitidea点击:

Getting "command not found" error in bash script

bash

提问by Pkp

I have written a bash script that gets three paths based on input parameters and then then gets the imagename/filename in the path.

我编写了一个 bash 脚本,它根据输入参数获取三个路径,然后获取路径中的图像名/文件名。

Something like:
I provide:

类似的东西:
我提供:

AA=/home/user

Then it uses the find command to get
/home/user/dir2/images/dir/tellmeimage1fun.bin

然后使用find命令获取
/home/user/dir2/images/dir/tellmeimage1fun.bin

Finally I have to get tellmeimage1fun.bin as output.

最后我必须得到 tellmeimage1fun.bin 作为输出。

Script:

脚本:

#!/bin/bash  

echo "arg0 n/k/d"  

AA=  
CC=  

PATH1="`find $AA/dir2/images/dir/ -name *image1*.bin`"  
PATH2="`find $AA/dir2/images/dir/ -name *bimage2*.bin`"  
PATH3="`find $AA/dir2/images/dir/ -name *cimage3*.bin`"  

if [ $CC = "n" ] ; then  
    PATH=$PATH1  
elif [ $CC = "k" ] ; then  
    PATH=$PATH2  
else  
    PATH=$PATH3  
fi  

#Getting filename name from path:  
IMG="`ls $PATH | cut -d "/" -f6`"

OUTPUT:  
/users/prasapat/bin/sl5: line 22: ls: command not found  
/users/prasapat/bin/sl5: line 22: cut: command not found  

If I give complete paths to ls and cut they work. But i don't want to do that for all commands in the script. If i remove the last line and echo the PATH variable it is completely fine. Only after adding the last command, I see the problem.

如果我给 ls 提供完整的路径并剪切它们就可以了。但我不想对脚本中的所有命令都这样做。如果我删除最后一行并回显 PATH 变量,则完全没问题。只有在添加最后一个命令后,我才看到问题。

What am I doing wrongly?

我做错了什么?

回答by Santiago Alessandri

The problem is that you are redefining the PATH variable where bash looks into to find the binary files if you don't use a complete path when calling.

问题是您正在重新定义 PATH 变量,如果您在调用时不使用完整路径,则 bash 会在其中查找二进制文件。

You should change the PATH in your bash script to MYPATH or something like that, so that it doesn't mess with the already environmental variables.

您应该将 bash 脚本中的 PATH 更改为 MYPATH 或类似的东西,这样它就不会与已经存在的环境变量混淆。

If you don't know what the PATH variable is for you can look at wikipedia's article

如果您不知道 PATH 变量是什么,您可以查看维基百科的文章

回答by Frederic De Groef

$PATHis a special environment variable that contains a list of directories where your shell (in this case, bash) should look in when you type a command (such as findand ls.) Just try echo $PATHin a script or in a shell to get a feeling of what it looks like (you will typically have /bin, /usr/binand /usr/local/binlisted there, maybe more.)

$PATH是一个特殊的环境变量,它包含一个目录列表,当您键入命令(例如findls)时,您的 shell(在本例中为 bash)应该查看的目录。只需echo $PATH在脚本或 shell 中尝试一下即可了解是什么它看起来像(你通常会有/bin/usr/bin/usr/local/bin在那里列出,也许更多。)

As you don't really need to redefine this variable in this particular script, you should use another name than $PATH.

由于您实际上并不需要在此特定脚本中重新定义此变量,因此您应该使用$PATH.

回答by John Kugelman

$PATHis a predefined variable which gives the directories to search when looking for executables. Pick a different variable name for your script and you'll be fine.

$PATH是一个预定义的变量,它提供了在查找可执行文件时要搜索的目录。为你的脚本选择一个不同的变量名,你会没事的。

回答by entropo

Use a different variable name than PATH. $PATHis the environment variable which tells your shell where to look for executables (so, e.g., you can run lsinstead of /bin/ls).

使用与PATH. $PATH是环境变量,它告诉您的 shell 在哪里查找可执行文件(例如,您可以运行ls而不是/bin/ls)。

回答by amit_g

You are using the PATHthat is special and used to locate the commands and that is why lscan't be resolved. Use any name other than PATH

您使用的PATH是特殊的,用于定位命令,这就是ls无法解析的原因。使用除PATH

if [ $CC = "n" ] ; then  
    MY_PATH=$PATH1  
elif [ $CC = "k" ] ; then  
    MY_PATH=$PATH2  
else  
    MY_PATH=$PATH3  
fi 

export MY_PATH

IMG="`ls $MY_PATH | cut -d "/" -f6`"

回答by Tom Walsh

I had this problem, turns out editing a bash script using Notepad++ was adding DOS line endings instead of UNIX line endings. Running the script in a Linux environment was causing the 'command not found' error to be thrown.

我遇到了这个问题,原来使用 Notepad++ 编辑 bash 脚本是添加 DOS 行结尾而不是 UNIX 行结尾。在 Linux 环境中运行脚本会导致抛出“未找到命令”错误。

Managed to diagnose the problem by running my script like so:

设法通过运行我的脚本来诊断问题,如下所示:

bash -x testscript.sh

Which will dump any compiler output. The error message that gets thrown is:

这将转储任何编译器输出。抛出的错误消息是:

bash -x testscript.sh
+ $'\r'
: command not found 2:
'estscript.sh: line 3: syntax error near unexpected token `{

I fixed the issue by changing the formatting of line endings in Notepad++ to be UNIX not DOS by going Edit -> EOL Conversion -> UNIX.

我通过编辑 -> EOL 转换 -> UNIX 将 Notepad++ 中行尾的格式更改为 UNIX 而不是 DOS,从而解决了该问题。