zoukankan      html  css  js  c++  java
  • shell 调试

    shell脚本的调试

    1. 运行脚本时,添加-x选项

    $ bash -x script-name
    $ bash -x domains.sh
    $ sh -x script_name
    

    2. 在脚本中开启编译选项

    #!/bin/bash
    clear
     
    # turn on debug mode
    set -x
    for f in *
    do
       file $f
    done
    # turn OFF debug mode
    set +x
    ls
    # more commands
    

    说明:

    set -x Prints the statements after interpreting metacharacters and variables
    set +x Stops the printing of statements
    set -v Prints the statements before interpreting metacharacters and variables
    set -f Disables file name generation (using metacharacters)

    3. 解释器参数

    #!/bin/bash -xv
    #!/bin/sh -x
    

    4.使用Debug变量和函数来实现

    #!/bin/bash
    _DEBUG="on"
    function DEBUG()
    {
     [ "$_DEBUG" == "on" ] &&  $@
    }
     
    DEBUG echo 'Reading files'
    for i in *
    do
      grep 'something' $i > /dev/null
      [ $? -eq 0 ] && echo "Found in $i file"
    done
    DEBUG set -x
    a=2
    b=3
    c=$(( $a + $b ))
    DEBUG set +x
    echo "$a + $b = $c"
    

    5. 使用日志打印

    (1.)下载log4shell.sh脚本

    https://github.com/fredpalmer/log4bash/blob/master/log4bash.sh
    

    (2.)日志打印

    #!/usr/bin/env bash
    source log4bash.sh
    
    log "This is regular log message... log and log_info do the same thing";
    
    log_warning "Luke ... you turned off your targeting computer";
    log_info "I have you now!";
    log_success "You're all clear kid, now let's blow this thing and go home.";
    log_error "One thing's for sure, we're all gonna be a lot thinner.";
    
    # If you have figlet installed -- you'll see some big letters on the screen!
    log_captains "What was in the captain's toilet?";
    
    # If you have the "say" command (e.g. on a Mac)
    log_speak "Resistance is futile";
    

    相关链接

    https://www.cyberciti.biz/tips/debugging-shell-script.html

    https://www.thegeekdiary.com/how-to-debug-shell-scripts/

    【励志篇】: 古之成大事掌大学问者,不惟有超世之才,亦必有坚韧不拔之志。
  • 相关阅读:
    Repeater使用二
    db2, oracle和sqlserver取前几行的语法
    AspNet 路径问题
    PL/Sql 中创建、调试、调用存储过程
    ORA错误编码
    PL/SQL 设置
    常用命令行
    SQL Server将单表数据导出成insert脚本形式
    获取Url链接内容
    Oracle安装注意事项
  • 原文地址:https://www.cnblogs.com/tomtellyou/p/15681852.html
Copyright © 2011-2022 走看看