美文网首页
如何解决执行sh -x xxx.sh出现:xxx.sh: 1:

如何解决执行sh -x xxx.sh出现:xxx.sh: 1:

作者: Aodongq1n | 来源:发表于2017-09-12 01:21 被阅读0次

    演示环境

    root@ubuntu:/bin# sudo lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 14.04 LTS
    Release:    14.04
    Codename:   trusty
    
    

    问题现象?

    root@ubuntu:/home/qpz/ShellCode# sh -x singleton.sh 
    singleton.sh: 1: singleton.sh: Syntax error: "(" unexpected
    

    为什么会出现这种问题?

    1. 刚开始以为是我脚本本身有语法错误,但是打开脚本细细看了下,并没有发现什么错误
    root@ubuntu:/home/qpz/ShellCode# cat singleton.sh 
    function keep_singleton()
    {
      local tmpfile="/tmp/cus_log_report$$"
      cat $tmpfile  
    }
    keep_singleton
    
    1. 于是我查看了系统的/bin/sh默认指向什么,如下:
    root@ubuntu:/bin# ls -l /bin/sh
    lrwxrwxrwx 1 root root 4 Sep 11 00:39 /bin/sh -> dash
    
    
    1. 这是我非常奇怪这个dash是个什么东西?于是我翻阅了一下书籍,查找dash是什么,并且总结了他们之间的不同,如下:

    dash的前身是ash,有人把 ash 从 NetBSD 移植到 Linux 并更名为 dash (Debian Almquist Shell),并建议将 /bin/sh 指向它,以获得更快的脚本执行速度。Dash Shell 比 Bash Shell 小的多,符合POSIX标准。

    1. 为什么在dash下会报语法错误?
    • bash: function在bash中为关键字
    • dash: dash中没有function这个关键字

    如何解决?

    1. 执行dpkg-reconfigure dash
    root@ubuntu:/home/qpz/ShellCode# sudo dpkg-reconfigure dash
    Removing 'diversion of /bin/sh to /bin/sh.distrib by dash'
    Adding 'diversion of /bin/sh to /bin/sh.distrib by bash'
    Removing 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by dash'
    Adding 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by bash'
    
    1. 弹出一个窗口,选择No
    解决方法.png
    1. 验证是否修改成功?
    root@ubuntu:/bin# ls -l /bin/sh
    lrwxrwxrwx 1 root root 4 Sep 11 00:39 /bin/sh -> bash
    
    
    1. 重新运行脚本
    root@ubuntu:/home/qpz/ShellCode# sh -x singleton.sh 
    + keep_singleton
    + local tmpfile=/tmp/cus_log_report5968
    + cat /tmp/cus_log_report5968
    cat: /tmp/cus_log_report5968: No such file or directory
    

    End.

    相关文章

      网友评论

          本文标题: 如何解决执行sh -x xxx.sh出现:xxx.sh: 1:

          本文链接:https://www.haomeiwen.com/subject/piuqsxtx.html