从 cron 作业执行 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/27887487/
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
"/bin/bash^M: bad interpreter: No such file or directory" error when executing a bash script from a cron job
提问by chalitha geekiyanage
Restore_DB.sh:-
恢复_DB.sh:-
#!/bin/bash
mysql -u user -ppassword DB_name < /home/A/B/SQL_File.sql
I used the above code to restore a MySQL database from a cron job but I'm getting the bellow error
我使用上面的代码从 cron 作业中恢复了 MySQL 数据库,但出现以下错误
/usr/local/cpanel/bin/jailshell: /home/A/B/Restore_DB.sh: /bin/bash^M: bad interpreter: No such file or directory
This is the cron job command I used:-
这是我使用的 cron 作业命令:-
/home/A/B/Restore_DB.sh
回答by stefreak
Try if dos2unix can fix your file:
试试 dos2unix 是否可以修复您的文件:
$ dos2unix /home/A/B/Restore_DB.sh
If dos2unix does not exist yet, you can install it with your distribution's package manager.
如果 dos2unix 尚不存在,您可以使用发行版的包管理器安装它。
The problem is the newline encoding, Windows/DOS encodes newlines differently than Unix.
问题在于换行符编码,Windows/DOS 对换行符的编码方式与 Unix 不同。
- Unix newline sequence:
\n
(only line feed character) - Windows newline sequence:
\r\n
(2 characters, carriage return and line feed)
- Unix 换行序列:(
\n
仅换行符) - Windows 换行顺序:
\r\n
(2 个字符,回车和换行)
回答by arkascha
his looks like a problem with different line end encodings on unixoid and MS-Windows like systems.
他看起来像是 unixoid 和 MS-Windows 类系统上不同行尾编码的问题。
Use the line ending \n
which is native to unixoid systems, not the MS-Windows style. That one holds an extra character which is typically displayed like what you see in the error message (^M
).
使用\n
unixoid 系统原生的行尾,而不是 MS-Windows 风格。那个包含一个额外的字符,通常显示为您在错误消息 ( ^M
) 中看到的内容。
You can take a closer look at the line in question by using a hexeditor. This allows you to see exactly what non-printable characters are used inside a string.
您可以使用十六进制编辑器仔细查看有问题的行。这使您可以准确查看字符串中使用了哪些不可打印的字符。
回答by Hahnemann
I just ran into this on OS X and noticed dos2unix is available as a brew formula:
我刚刚在 OS X 上遇到了这个问题,并注意到 dos2unix 可用作 brew 公式:
brew install dos2unix
回答by SanthoshSally
yum install dos2unix
yum install dos2unix
works like a charm !!!!
奇迹般有效 !!!!