linux的shell脚本报错:-bash: ./infos.sh: /usr/bin/bash^M: bad interpreter: No such file or directory

分析报错:-bash: ./infos.sh: /usr/bin/bash^M: bad interpreter: No such file or directory
或-bash: ./infos.sh: /bin/bash^M: bad interpreter: No such file or directory

分析:因为行尾部的结束符问题,一般在用windows系统上编辑器如sublime,保存到linux系统中的shell文件,在执行的时候报错。
此时我们需要修改行尾标记,以sublime为案例单击 View > Line Endings > Unix,之后再次保存shell文件到服务执行就可以了。
下面是一些英文资料,可作深入研究:

This is caused by editing file in windows and importing and executing in unix.
528

I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.

Try running dos2unix on the script:

http://dos2unix.sourceforge.net/

Or just rewrite the script in your Unix env using vi and test.

Unix uses different line endings so can’t read the file you created on Windows. Hence it is seeing ^M as an illegal character.

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.

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

settings->preferences
new document / default directory tab
select the format as unix and close
create a new document

发布了16 篇原创文章 · 获赞 0 · 访问量 661

猜你喜欢

转载自blog.csdn.net/yaqiang2017/article/details/104088247