zoukankan      html  css  js  c++  java
  • bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory的解决方法------dos--->unix

     一些人喜欢用vim来写linux shell script, 但是, 有的人喜欢在Windows下用一些方便的编辑器(比如鼎鼎大名的Notepad++)写好, 然后拷贝文件到linux下, 结果呢, 在执行脚本a.sh的时候, 会出现如下问题:

    [plain] view plain copy
     
    1. [taoge@localhost learn_shell]$ ./a.sh   
    2. bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory  
    3. [taoge@localhost learn_shell]$   


          什么原因呢, 我们有理由怀疑是文件格式问题? 我们用vim a.sh进入a.sh这个文件, 然后在底部模式下, 执行:set ff查看一下, 结果发现fileformat=dos, 看看, 果然是文件格式问题, 那怎么解决呢?

         方法一:vim a.sh进入a.sh后, 在底部模式下, 执行:set fileformat=unix后执行:x或者:wq保存修改。 然后就可以执行./a.sh运行脚本了。(我亲自试过, 是ok的)

         方法二:直接执行sed -i "s/ //" a.sh来转化, 然后就可以执行./a.sh运行脚本了。(我亲自试过, 是ok的)

         方法三:直接执行dos2unix a.sh来转化, 然后就可以执行./a.sh运行脚本了。(我的linux上执行dos2unix ./a.sh失败, 但是不要放弃啊, 加个busybox就可以了), 如下:

    [plain] view plain copy
     
    1. dos2unix a.sh   
    2. bash: dos2unix: command not found  
    3. [taoge@localhost learn_shell]$ busybox dos2unix a.sh   
    4. [taoge@localhost learn_shell]$  

          实际上, 经过上述三种方法修改后, 我们都可以再用:set ff再查一下, 发现a.sh的fileformat果然是unix了。   第三种方法最方便, 建议用第三种!

  • 相关阅读:
    UWP开发必备:常用数据列表控件汇总比较
    CodeForces 372 A. Counting Kangaroos is Fun
    ubuntu 13.10 eclipse 菜单栏不可用的问题
    Codeforces Round #219(Div. 2)373 B. Making Sequences is Fun(二分+找规律)
    Git/Github使用方法小记
    Ubuntu 下jdk的安装
    VIM简明教程
    codeforces 371 C-Hamburgers
    codeforces 371A K-Periodic Array
    计算机网络中IP地址和MAC地址
  • 原文地址:https://www.cnblogs.com/zhoading/p/9080919.html
Copyright © 2011-2022 走看看