zoukankan      html  css  js  c++  java
  • shell中脚本调试----学习

    1、使用dos2unix命令处理在windows下开发的脚本

    将windows下编辑的脚本放置到linux下执行的情况如下:

    复制代码
    [root@ks ~]# cat -v nginx.sh 
    #!/bin/bash^M
    a=1 n^M
    sum=0    ^M
    while ((a <=50 ))^M
    do^M
        ((sum=sum+a)) ^M
        ((a++)) ^M
    done^M

    [root@ks ~]# sh nginx.sh     #执行脚本 
    : command not found
    : command not found
    'ginx.sh: line 4: syntax error near unexpected token `
    'ginx.sh: line 4: `while ((i <=100 ))

    复制代码

    在上述的过程中,在windows下开发的脚本,检查没有问题。但在linux系统中执行时出现莫名其妙的语法错误。这时,最好执行dos2unix格式化一下。

    复制代码
    #格式化命令安装
    [root@ks ~]# yum -y install dos2unix
    #使用方法
    [root@ks ~]# dos2unix nginx.sh 
    dos2unix: converting file nginx.sh to UNIX format ...
    #再次查看

    [root@ks ~]# cat -v nginx.sh
    #!/bin/bash
    a=1 n
    sum=0
    while ((a <=50 ))
    do
    ((sum=sum+a))
    ((a++))
    done

    复制代码

    提示:^M消失了,说明已正常。windows下代码的换行符合linux下的不一样,导致了本例的问题。在windows下开发的脚本或非自己所写,需要使用dos2unix格式化,防止执行过程中出现错误。

    不仅仅.sh脚本可以转换,其他.txt等win上文件也可以通过该命令转换

    2、使用bash命令参数调试

    复制代码
    [root@ks ~]# sh [-nvx] nginx.sh
    
    参数说明:
    -n:不会执行该脚本,仅查询脚本语法是否有问题,并给出错误提示
    -v:在执行脚本时,先将脚本的内容输出到屏幕上,然后执行脚本。如果有错误,也会给出错误提示。
    -x:将执行的脚本内容及输出显示到屏幕上,这是对调试很有用的参数。
    复制代码

    注:参数-x是追踪脚本执行过程一种非常好的方法,他可以在执行前列出所执行的所有程序段。

           如果程序段落,在输出时,最前面会加上+符号,表示程序代码。

           如果执行脚本发生问题(非语法问题时),利用-x参数,就可以知道问题出在哪一行

           一般情况下如果是调试逻辑错误的脚本,用-x的参数效果更佳。

          缺点:加载系统函数库等很多我们不想查看其整个过程的脚本时,会有太多输出,导致很难查看所需要的内容。

    3、使用echo命令调试

    echo命令是最有用的调试脚本的工具之一。一般应在可能出现的问题的脚本重要的部分加入echo命令(在变量读取或修改操作的前后加入echo命令,并紧挨着退出命令exit)。此调试方法不仅适用于shell,在php、Python语言中经常使用。

    范例:

    复制代码
    [root@ks ~]# cat  debug.sh
    #!/bin/bash
    read -p "please input two number:" a b
    echo $a $b     #输出变量,查看获取的变量值
    exit           #退出脚本,不继续执行脚本。
    
    if (($a < $b))
      then
        echo "$a<$b"
    elif (($a == $b))
      then
        echo "$a=$b"
    else
        echo "$a>$b"
    fi
    复制代码

    4、使用set命令调试部分脚本内容

    set 命令也可以用于辅助脚本调试。

    set命令常用的调试选项:

    • set -n :读取命令单并不执行
    • set -v :显示读取的所有行
    • set -x :显示所有命令及其参数

    提示:通过set -x 命令开启调试功能,而通过set +x 关闭调试功能。

    set命令最大优点是,和bash -x相比,set -x可以缩小调试的作用域。

    复制代码

    [root@ks ~]# cat debug.sh

    #!/bin/bash
    read -p "please input two number:" a b
    set -x    #开启调试
    if (($a < $b))
      then
        echo "$a<$b"
    elif (($a == $b))
      then
        echo "$a=$b"
    else
        echo "$a>$b"
    fi
    set +x    #结束调试,只针对set -x 和set +x 之间的脚本进行调试
    
    echo "完美"
    复制代码

    执行脚本查看调试结果:

    复制代码
    
    [root@ks ~]# sh debug.sh 
    please input two number:2 2
    + (( 2 < 2 ))
    + (( 2 == 2 ))
    + echo 2=2
    2=2
    + set +x
    完美
    
    提示:加了set +x,在运行脚本的时候,无需使用sh -x 。

    参考:https://www.cnblogs.com/freeblogs/p/7819767.html

  • 相关阅读:
    cf D. Vessels
    cf C. Hamburgers
    zoj 3758 Singles' Day
    zoj 3777 Problem Arrangement
    zoj 3778 Talented Chef
    hdu 5087 Revenge of LIS II
    zoj 3785 What day is that day?
    zoj 3787 Access System
    判断给定图是否存在合法拓扑排序
    树-堆结构练习——合并果子之哈夫曼树
  • 原文地址:https://www.cnblogs.com/clarenceyang/p/10683687.html
Copyright © 2011-2022 走看看