Bash 脚本和 /bin/bash^M: bad interpreter: No such file or directory
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14219092/
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 and /bin/bash^M: bad interpreter: No such file or directory
提问by cartonn
I'm learning through this tutorial to learn bash scriptsto automate a few tasks for me. I'm connecting to a server using putty.
我正在通过本教程学习bash 脚本来为我自动化一些任务。我正在使用腻子连接到服务器。
The script, located in .../Documents/LOG
, is:
位于 中的脚本.../Documents/LOG
是:
#!/bin/bash
# My first script
echo "Hello World!"
And I executed the following for read/write/execute permissions
我执行了以下操作 read/write/execute permissions
chmod 755 my_script
Then, when I enter ./my_script
, I'm getting the error given in the title.
然后,当我输入时./my_script
,我收到标题中给出的错误。
Some similar questions wanted to see these, so I think they might help:
一些类似的问题想看看这些,所以我认为它们可能会有所帮助:
which bash
/bin/bash
/bin/bash
and
和
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/bin/mh
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/bin/mh
I tried adding current directory to PATH
, but that doesn't work..
我尝试将当前目录添加到PATH
,但这不起作用..
回答by cowls
I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.
在 Windows env 中创建脚本然后移植到 Unix 环境中运行时,我已经看到了这个问题。
Try running dos2unix
on the script:
尝试dos2unix
在脚本上运行:
http://dos2unix.sourceforge.net/
http://dos2unix.sourceforge.net/
Or just rewrite the script in your Unix env using vi
and test.
或者只是使用vi
和测试在您的 Unix 环境中重写脚本。
Unix uses different line endings so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.
Unix 使用不同的行结尾,因此无法读取您在 Windows 上创建的文件。因此它将 ^M 视为非法字符。
If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.
如果您想在 Windows 上编写文件然后移植,请确保您的编辑器设置为以 UNIX 格式创建文件。
In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say Dos\Windows
. To change it go to
在屏幕右下角的记事本++中,它会告诉您文档格式。默认情况下,它会说Dos\Windows
. 要改变它去
- settings->preferences
- new document / default directory tab
- select the format as unix and close
- create a new document
- 设置->首选项
- 新文档/默认目录选项卡
- 选择格式为 unix 并关闭
- 创建一个新文档
回答by Nivin V Joseph
Run following command in terminal
在终端中运行以下命令
sed -i -e 's/\r$//' scriptname.sh
Then try
然后试试
./scriptname.sh
It should work.
它应该工作。
回答by Mario Campa
If you use Sublime Texton Windows or Mac to edit your scripts:
如果你在 Windows 或 Mac 上使用Sublime Text来编辑你的脚本:
Click on View > Line Endings > Unix
and savethe file again.
单击View > Line Endings > Unix
并再次保存文件。
回答by StonecoldIM
This is caused by editing file in windows and importing and executing in unix.
这是在windows下编辑文件,在unix下导入执行造成的。
dos2unix -k -o filename
should do the trick.
dos2unix -k -o filename
应该做的伎俩。
回答by Urik
In notepad++ you can set it for the file specifically by pressing
在记事本++中,您可以通过按专门为文件设置它
Edit --> EOL Conversion --> UNIX/OSX Format
编辑 --> EOL 转换 --> UNIX/OSX 格式
回答by Md. Shafiqur Rahman
problem is with dos line ending. Following will convert it for unix
问题在于 dos 行结束。以下将其转换为unix
dos2unix file_name
NB: you may need to install dos2unix first with yum install dos2unix
注意:您可能需要先安装 dos2unix yum install dos2unix
another way to do it is using sed
command to search and replace the dos line ending characters to unix format:
另一种方法是使用sed
命令搜索并将 dos 行结束字符替换为 unix 格式:
$sed -i -e 's/\r$//' your_script.sh
回答by kalyani chaudhari
Your file has Windows line endings, which is confusing Linux.
您的文件有 Windows 行结尾,这让 Linux 感到困惑。
Remove the spurious CR characters. You can do it with the following command:
删除虚假的 CR 字符。您可以使用以下命令执行此操作:
$ sed -i -e 's/\r$//' setup.sh
回答by NWRichmond
I was able to resolve the issue by opening the script in Geditand saving it with the proper Line Ending
option:
我能够通过在Gedit 中打开脚本并使用正确的Line Ending
选项保存它来解决这个问题:
File > Save As...
文件 > 另存为...
In the bottom left of the Save As
prompt, there are drop-down menus for Character Encoding and Line Ending. Change the Line Ending from Windows
to Unix/Linux
then Save.
在Save As
提示的左下角,有字符编码和行尾的下拉菜单。将 Line Ending 从 更改Windows
为Unix/Linux
然后保存。
回答by Christos Lytras
For Eclipseusers, you can either change the file encoding directly from the menu File > Convert Line Delimiters To > Unix (LF, \n, 0Α, ?)
:
对于Eclipse用户,您可以直接从菜单更改文件编码File > Convert Line Delimiters To > Unix (LF, \n, 0Α, ?)
:
Or change the New text file line delimiter
to Other: Unix
on Window > Preferences > General > Workspace
panel:
或更改New text file line delimiter
到Other: Unix
上Window > Preferences > General > Workspace
面板:
回答by tuanh118
Atom has a built-in line ending selector package
Atom 有一个内置的行尾选择器包
More details here: https://github.com/atom/line-ending-selector