• 【Linux学习】解决Linux环境下执行脚本时报错:/bin/bash^M: 坏的解释器: 没有那个文件或目录


    一、问题描述

    我在Windows 10 系统下编辑了一个发送消息到企业微信的shell脚本文件,然后copy到了远程的Linux服务器,当运行的时候报错了。如下所示:
    在这里插入图片描述

    root@ubuntu116:/data/gitlabData/auto_back_shell# ./qiyewechat-notifier.sh 
    -bash: ./qiyewechat-notifier.sh: /bin/bash^M: 坏的解释器: 没有那个文件或目录
    root@ubuntu116:/data/gitlabData/auto_back_shell# 
    • 1
    • 2
    • 3

    二、错误原因

    这个文件在Windows 下编辑过,在Windows下每一行结尾是 ,而Linux下则是 ,所以才会有 多出来的 。

    三、修改错误

    使用指令sed -i 's/ $//' xxxxxxx.sh,上面的指令会把 xxxxxxx.sh 中的 替换成空白!
    实操一下:
    在这里插入图片描述

    root@ubuntu116:/data/gitlabData/auto_back_shell# sed -i 's/
    $//' qiyewechat-notifier.sh 
    您在 /var/mail/root 中有新邮件
    root@ubuntu116:/data/gitlabData/auto_back_shell# ./qiyewechat-notifier.sh -h 
    ./qiyewechat-notifier.sh: 非法选项 -- h
    Usage:
      qiyewechat.sh [-u USER] [-t TITLE] [-c CONTENT] [-d DETAIL] [-p PICTURE]
    Description:
        USER, 用户.
        TITLE, 标题.
        CONTENT, 内容.
        DETAIL, 细节.
        PICTURE, 图片.
    root@ubuntu116:/data/gitlabData/auto_back_shell# 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    如上所示,执行了下面的脚本之后,

    sed -i 's/
    $//' qiyewechat-notifier.sh
    • 1

    qiyewechat-notifier.sh就可以正常运行了!

    四、附录

    qiyewechat-notifier.sh的部分代码如下所示:

    在这里插入图片描述

    #!/bin/bash
    
    #用法提示
    usage() {
        echo "Usage:"
        echo "  qiyewechat.sh [-u USER] [-t TITLE] [-c CONTENT] [-d DETAIL] [-p PICTURE]"
        echo "Description:"
        echo "    USER, 用户."
        echo "    TITLE, 标题."
        echo "    CONTENT, 内容."
        echo "    DETAIL, 细节."
        echo "    PICTURE, 图片."
        exit -1
    }
    
    
    # 获取脚本执行时的选项
    while getopts u:t:c:d:p: option
    do
       case "${option}"  in
                    u) USER=${OPTARG};;
                    t) TITLE=${OPTARG};;
                    c) CONTENT=${OPTARG};;
                    d) DETAIL=${OPTARG};;
                    p) PICTURE=${OPTARG};;
                    h) usage;;
                    ?) usage;;
       esac
       echo $option
       echo $OPTARG
    
    done
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
    转载请保留原文地址:https://blog.csdn.net/qq446282412/article/details/86488451
    ☞ 本人QQ: 3024665621
    ☞ QQ交流群: 123133153
    github.com/ouyangpeng
    oypcz@foxmail.com


  • 相关阅读:
    synchronized关键字的用法
    for循环删除集合陷阱
    Java之可变参数
    下拉菜单中的Option对象
    JavaScript数组
    线程
    尝试用代码写博客
    环境配置大全
    3中边缘检测算子
    caffe新手入门
  • 原文地址:https://www.cnblogs.com/xiondun/p/12498373.html
Copyright © 2020-2023  润新知