Bash脚本和/ bin / bash ^ M:错误的解释器:没有这样的文件或目录[重复]

本文翻译自:Bash script and /bin/bash^M: bad interpreter: No such file or directory [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

  • Are shell scripts sensitive to encoding and line endings? Shell脚本对编码和行尾是否敏感? 5 answers 5个答案

I'm learning through this tutorial to learn bash scripts to automate a few tasks for me. 我正在学习本教程,学习bash脚本以自动执行一些任务。 I'm connecting to a server using putty. 我正在使用腻子连接到服务器。

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 然后我执行以下读取/写入/执行权限

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 ,但这不起作用。


#1楼

参考:https://stackoom.com/question/xf2C/Bash脚本和-bin-bash-M-错误的解释器-没有这样的文件或目录-重复


#2楼

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和test在Unix env中重写脚本。

Unix uses different line endings so can't read the file you created on Windows. Unix使用不同的行尾,因此无法读取您在Windows上创建的文件。 Hence it is seeing ^M as an illegal character. 因此,它将^ 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. 在屏幕右下方的notepad ++中,它告诉您文档格式。 By default, it will say Dos\\Windows . 默认情况下,它将显示Dos\\Windows To change it go to 要更改它,请转到

  • settings->preferences 设置->首选项
  • new document / default directory tab 新文档/默认目录选项卡
  • select the format as unix and close 选择unix格式并关闭
  • create a new document 创建一个新文件

#3楼

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应该可以解决问题。


#4楼

If you use Sublime Text on Windows or Mac to edit your scripts: 如果您在Windows或Mac上使用Sublime Text来编辑脚本,请执行以下操作:

Click on View > Line Endings > Unix and save the file again. 单击View > Line Endings > Unix然后再次保存文件。


#5楼

In notepad++ you can set it for the file specifically by pressing 在notepad ++中,您可以通过按

Edit --> EOL Conversion --> UNIX/OSX Format 编辑-> EOL转换-> UNIX / OSX格式


#6楼

Run following command in terminal 在终端中运行以下命令

sed -i -e 's/\r$//' scriptname.sh

Then try 然后尝试

./scriptname.sh

It should work. 它应该工作。

你可能感兴趣的:(bash)