Bash Line Number Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us bash print line number on error Learn more about Stack Overflow the company Business Learn more about hiring developers
Bash Line Number Of File
or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack bash line number variable Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to show line number when executing
Bash Get Line Number
bash script up vote 18 down vote favorite 8 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 would stop when error occurs. However, it's still rather difficult for me to locate which line did the execution stop in order to locate the problem. Is there bash get line number of string a method which can output the line number of the script before each line is executed? Or output the line number before the command exhibition generated by set -x? Or any method which can deal with my script line location problem would be a great help. Thanks. linux bash testing share|improve this question edited Aug 25 '13 at 13:21 devnull 60.7k16102135 asked Jul 23 '13 at 7:31 dspjm 1,03321226 add a comment| 3 Answers 3 active oldest votes up vote 42 down vote accepted 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_SOU
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of
Bash Get Line Number Of Match
this site About Us Learn more about Stack Overflow the company Business
Bash Grep Line Number
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask bash echo line number Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up http://stackoverflow.com/questions/17804007/how-to-show-line-number-when-executing-bash-script How to get the real line number of a failing Bash command? up vote 2 down vote favorite In the process of coming up with a way to catch errors in my Bash scripts, I've been experimenting with "set -e", "set -E", and the "trap" command. In the process, I've discovered some strange behavior in how $LINENO is evaluated in the context of http://stackoverflow.com/questions/24398691/how-to-get-the-real-line-number-of-a-failing-bash-command functions. First, here's a stripped down version of how I'm trying to log errors: #!/bin/bash set -E trap 'echo Failed on line: $LINENO at command: $BASH_COMMAND && exit $?' ERR Now, the behavior is different based on where the failure occurs. For example, if I follow the above with: echo "Should fail at: $((LINENO + 1))" false I get the following output: Should fail at: 6 Failed on line: 6 at command: false Everything is as expected. Line 6 is the line containing the single command "false". But if I wrap up my failing command in a function and call it like this: function failure { echo "Should fail at $((LINENO + 1))" false } failure Then I get the following output: Should fail at 7 Failed on line: 5 at command: false As you can see, $BASH_COMMAND contains the correct failing command: "false", but $LINENO is reporting the first line of the "failure" function definition as the current command. That makes no sense to me. Is there a way to get the line number of the line referenced in $BASH_COMMAND? It's possible this behavior is specific
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers http://stackoverflow.com/questions/29081531/shell-script-print-line-number-when-it-errors-out or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x http://linuxcommand.org/wss0150.php Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Shell script print line number when it errors out up vote 2 down vote favorite I have been looking for a way to print the line number inside the shell script when it errors line number out. I came across '-x' option, which prints the line when running the shell script, but this is not exactly what I want. Maybe I could do $LINENO before every exit code? Is there a cleaner way to do it? I just wanted the line number so I could open the shell script and directly go to the place where the interpreter realized the error. bash shell scripting share|improve this question asked Mar 16 '15 at 16:06 user2441441 3131419 add bash line number a comment| 1 Answer 1 active oldest votes up vote 3 down vote accepted Using PS4=':$LINENO+' will add line number to the output of set -x. If you only want to print that on errors, there's some risk of running into bugs in recent interpreters. However, you can try the following (first given in this previous answer): error() { local parent_lineno="$1" local message="$2" local code="${3:-1}" if [[ -n "$message" ]] ; then echo "Error on or near line ${parent_lineno}: ${message}; exiting with status ${code}" else echo "Error on or near line ${parent_lineno}; exiting with status ${code}" fi exit "${code}" } trap 'error ${LINENO}' ERR Again, this will not work on some recent builds of bash, which don't always have LINENO set correctly inside traps. Another approach (which will only work on recent shells; the below uses some bash 4.0 and 4.1 features) is to use PS4 to emit the exit status and line number of each command to a dedicated file descriptor, and use tail to print only the last line given to that FD before the shell exits: exec {BASH_XTRACEFD}> >(tail -n 1) # send set -x output to tail -n 1 PS4=':At line $LINENO; prior command exit status $?+' set -x share|improve this answer edited Mar 16 '15 at 16:15 answered Mar 16 '15 at 16:08 Charles Duffy 94.5k15102142 It is risky if this shell script fails because of bash versions because it will
and Signals and Traps (Oh My!) - Part 1 by William Shotts, Jr. In this lesson, we're going to look at handling errors during the execution of your scripts. The difference between a good program and a poor one is often measured in terms of the program's robustness. That is, the program's ability to handle situations in which something goes wrong. Exit status As you recall from previous lessons, every well-written program returns an exit status when it finishes. If a program finishes successfully, the exit status will be zero. If the exit status is anything other than zero, then the program failed in some way. It is very important to check the exit status of programs you call in your scripts. It is also important that your scripts return a meaningful exit status when they finish. I once had a Unix system administrator who wrote a script for a production system containing the following 2 lines of code: # Example of a really bad idea cd $some_directory rm * Why is this such a bad way of doing it? It's not, if nothing goes wrong. The two lines change the working directory to the name contained in $some_directory and delete the files in that directory. That's the intended behavior. But what happens if the directory named in $some_directory doesn't exist? In that case, the cd command will fail and the script executes the rm command on the current working directory. Not the intended behavior! By the way, my hapless system administrator's script suffered this very failure and it destroyed a large portion of an important production system. Don't let this happen to you! The problem with the script was that it did not check the exit status of the cd command before proceeding with the rm command. Checking the exit status There are several ways you can get and respond to the exit status of a program. First, you can examine the contents of the $? environment variable. $? will contain the exit status of the last command executed. You can see this work with the following: [me] $ true; echo $? 0 [me] $ false; echo $? 1 The true and false commands are programs that do nothing except return an exit status of zero and one, respectively. Using them, we can see how the $? environment variable contains the exit status of the previous program. So to check the exit status, we could write the script this way: # Check the exit status cd $some_directory if [ "$?" = "0" ]; then rm * else echo