How to show line number when executing bash script

后端 未结 4 871
一向
一向 2020-12-07 08:25

I have a test script which has a lot of commands and will generate lots of output, I use set -x or set -v and set -e, so the script wo

相关标签:
4条回答
  • 2020-12-07 08:35

    Simple (but powerful) solution: Place echo around the code you think that causes the problem and move the echo line by line until the messages does not appear anymore on screen - because the script has stop because of an error before.

    Even more powerful solution: Install bashdb the bash debugger and debug the script line by line

    0 讨论(0)
  • 2020-12-07 08:42

    In Bash, $LINENO contains the line number where the script currently executing.

    If you need to know the line number where the function was called, try $BASH_LINENO. Note that this variable is an array.

    For example:

    #!/bin/bash       
    
    function log() {
        echo "LINENO: ${LINENO}"
        echo "BASH_LINENO: ${BASH_LINENO[*]}"
    }
    
    function foo() {
        log "$@"
    }
    
    foo "$@"
    

    See here for details of Bash variables.

    0 讨论(0)
  • 2020-12-07 08:44

    You mention that you're already using -x. The variable PS4 denotes the value is the prompt printed before the command line is echoed when the -x option is set and defaults to : followed by space.

    You can change PS4 to emit the LINENO (The line number in the script or shell function currently executing).

    For example, if your script reads:

    $ cat script
    foo=10
    echo ${foo}
    echo $((2 + 2))
    

    Executing it thus would print line numbers:

    $ PS4='Line ${LINENO}: ' bash -x script
    Line 1: foo=10
    Line 2: echo 10
    10
    Line 3: echo 4
    4
    

    http://wiki.bash-hackers.org/scripting/debuggingtips gives the ultimate PS4 that would output everything you will possibly need for tracing:

    export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
    
    0 讨论(0)
  • 2020-12-07 09:00

    Workaround for shells without LINENO

    In a fairly sophisticated script I wouldn't like to see all line numbers; rather I would like to be in control of the output.

    Define a function

    echo_line_no () {
        grep -n "$1" $0 |  sed "s/echo_line_no//" 
        # grep the line(s) containing input $1 with line numbers
        # replace the function name with nothing 
    } # echo_line_no
    

    Use it with quotes like

    echo_line_no "this is a simple comment with a line number"
    

    Output is

    16   "this is a simple comment with a line number"
    

    if the number of this line in the source file is 16.

    This basically answers the question How to show line number when executing bash script for users of ash or other shells without LINENO.

    Anything more to add?

    Sure. Why do you need this? How do you work with this? What can you do with this? Is this simple approach really sufficient or useful? Why do you want to tinker with this at all?

    Want to know more? Read reflections on debugging

    0 讨论(0)
提交回复
热议问题