Linux Bash 脚本在空行上打印“未找到命令”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7362504/
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
Bash script prints "Command Not Found" on empty lines
提问by David
Every time I run a script using bash scriptname.sh
from the command line in Debian, I get Command Not found
and then the result of the script.
每次我bash scriptname.sh
从 Debian 的命令行运行脚本时,我都会得到Command Not found
脚本的结果。
The script works but there is always a Command Not Found
statement printed on screen for each empty line. Each blank line is resulting in a command not found.
该脚本有效,但Command Not Found
屏幕上总是为每个空行打印一条语句。每个空行都会导致找不到命令。
I am running the script from the /var
folder.
我正在从/var
文件夹运行脚本。
Here is the script:
这是脚本:
#!/bin/bash
echo Hello World
I run it by typing the following:
我通过键入以下内容来运行它:
bash testscript.sh
Why would this occur?
为什么会出现这种情况?
采纳答案by chown
Make sure your first line is:
确保您的第一行是:
#!/bin/bash
Enter your path to bash if it is not /bin/bash
如果不是,请输入您的 bash 路径 /bin/bash
Try running:
尝试运行:
dos2unix script.sh
That wil convert line endings, etc from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF)
to \n (LF)
.
这会将行尾等从 Windows 转换为 unix 格式。即它从行尾去除 \r (CR) 以将它们从 更改\r\n (CR+LF)
为\n (LF)
。
More details about the dos2unix
command (man page)
Another way to tell if your file is in dos/Win format:
另一种判断文件是否为 dos/Win 格式的方法:
cat scriptname.sh | sed 's/\r/<CR>/'
The output will look something like this:
输出将如下所示:
#!/bin/sh<CR>
<CR>
echo Hello World<CR>
<CR>
This will output the entire file text with <CR>
displayed for each \r
character in the file.
这将输出整个文件文本,<CR>
并为文件中的每个\r
字符显示。
回答by clyfish
You can use bash -x scriptname.sh
to trace it.
你可以用bash -x scriptname.sh
它来追踪它。
回答by paxdiablo
If the script does its job (relatively) well, then it's running okay. Your problem is probably a single line in the file referencing a program that's either not on the path, not installed, misspelled, or something similar.
如果脚本(相对)很好地完成了它的工作,那么它运行正常。您的问题可能是文件中的一行引用了不在路径上、未安装、拼写错误或类似的程序。
One way is to place a set -x
at the top of your script or run it with bash -x
instead of just bash
- this will output the lines before executing them and you usually just need to look at the command output immediately before the error to see what's causing the problem
一种方法是将 aset -x
放在脚本的顶部或运行它bash -x
而不是仅仅运行它bash
- 这将在执行它们之前输出这些行,您通常只需要在错误发生之前立即查看命令输出以查看导致问题的原因
If, as you say, it's the blank lines causing the problems, you might want to check what's actaully inthem. Run:
如果像你说的,它的空行造成的问题,你可能要检查什么actaully在他们。跑:
od -xcb testscript.sh
and make sure there's no "invisible" funny characters like the CTRL-M
(carriage return) you may get by using a Windows-type editor.
并确保没有像CTRL-M
使用 Windows 类型的编辑器可能获得的(回车)这样的“隐形”有趣字符。
回答by bash-o-logist
use dos2unix
on your script file.
使用dos2unix
你的脚本文件。
回答by Lypso345
I also ran into a similar issue. The issue seems to be permissions. If you do an ls -l
, you may be able to identify that your file may NOT have the execute bit turned on. This will NOT allow the script to execute. :)
我也遇到了类似的问题。问题似乎是权限。如果您执行ls -l
,您可能能够确定您的文件可能没有打开执行位。这将不允许脚本执行。:)
As @artooro added in comment:
正如@artooro 在评论中添加的那样:
To fix that issue run
chmod +x testscript.sh
要解决该问题,请运行
chmod +x testscript.sh
回答by Stefan van den Akker
I was also having some of the Cannot execute command
. Everything looked correct, but in fact I was having a non-breakable space
right before my command which was ofcourse impossible to spot with the naked eye:
我也有一些Cannot execute command
。一切看起来都正确,但实际上我
在我的命令之前有一个不可破坏的空间,这当然不可能用肉眼发现:
if [[ "true" ]]; then
highlight --syntax js "var i = 0;"
fi
Which, in Vim, looked like:
在 Vim 中,它看起来像:
if [[ "true" ]]; then
highlight --syntax js "var i = 0;"
fi
Only after running the Bash script checker shellcheck
did I find the problem.
只有在运行 Bash 脚本检查器shellcheck
后,我才发现问题。
回答by Aminah Nuraini
Try chmod u+x testscript.sh
尝试 chmod u+x testscript.sh
I know it from here: http://www.linuxquestions.org/questions/red-hat-31/running-shell-script-command-not-found-202062/
我从这里知道:http: //www.linuxquestions.org/questions/red-hat-31/running-shell-script-command-not-found-202062/
回答by Masood Moghini
for executing that you must provide full path of that for example
要执行该操作,您必须提供完整路径,例如
/home/Manuel/mywrittenscript
回答by Eugene Cuz
Had the same problem. Unfortunately
有同样的问题。很遗憾
dos2unix winfile.sh
bash: dos2unix: command not found
so I did this to convert.
所以我这样做是为了转换。
awk '{ sub("\r$", ""); print }' winfile.sh > unixfile.sh
and then
进而
bash unixfile.sh
回答by Marek Wysocki
Problems with running scripts may also be connected to bad formatting of multi-line commands, for example if you have a whitespace character after line-breaking "\". E.g. this:
运行脚本的问题也可能与多行命令的格式错误有关,例如,如果换行符“\”后有空格字符。例如这个:
./run_me.sh \
--with-some parameter
(please note the extra space after "\") will cause problems, but when you remove that space, it will run perfectly fine.
(请注意“\”后面的额外空格)会导致问题,但是当您删除该空格时,它会运行得很好。