执行shell脚本提示/bin/bash^M: bad interpreter: No such file or directory
今天在Linux上执行脚本,提示:./random.sh
-bash:./random.sh: /bin/bash^M: bad interpreter: No such file or directory
通过file命令查看文件格式:
file random.sh
random.sh: Bourne-Again shell script, UTF-8 Unicode text executable, with CRLF line terminators
可以看到分隔符是CRLF格式,这是Windows格式的换行符,Linux不识别。
通过cat命令可以查看文件的分隔符:
cat -v random.sh
#!/bin/bash^M
每行都多了一个^M,Linux不识别,解决这个问题,需要两步:
①对于已经存在的文件,比如randomsh,使用Notepad++编辑器打开,选中全部内容,然后点击"编辑"…-》"文档格式转换"-》“转换为UNIX格式",保存。
②由于我的shell文件是在PyCharm中编写的,因此对于之后新建的shell脚本,也需要使用Unix格式,打开PyCharm ,点击File-->Settings-->Editor-->Code style,在右侧的line separator中选择Unix and OS X(\n) ,修改后保存配用。
浙公网安备 33010602011771号