Ubuntu运行*.sh文件出现 bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory 问题解决方法

用Windows下用一些方便的编辑器(比如鼎鼎大名的Notepad++,Sublim)写好, 然后拷贝文件到linux下, 结果呢, 在执行脚本*.sh的时候, 会出现如下问题:

[taoge@localhost learn_shell]$ ./a.sh   
bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory  
[taoge@localhost learn_shell]$

用vim .sh进入.sh这个文件,然后在底部模式下, 执行 :set ff 查看一下, 结果发现fileformat=dos, 看看, 果然是文件格式问题

方案一:

vim .sh进入.sh后,在底部模式下,执行:set fileformat=unix 后执行:x或者:wq保存修改。 然后就可以执行./*.sh运行脚本了。

方案二:

直接执行sed -i "s/\r//" a.sh来转化, 然后就可以执行./a.sh运行脚本了。

方案三:

直接执行dos2unix a.sh来转化, 然后就可以执行./a.sh运行脚本了。(外加busybox),如下:

    dos2unix a.sh   
    bash: dos2unix: command not found  
    [taoge@localhost learn_shell]$ busybox dos2unix a.sh   
    [taoge@localhost learn_shell]$  

refrences

posted @ 2020-10-23 11:56  Sunny_SunShine  阅读(425)  评论(0编辑  收藏  举报