How to get the real line number of a failing Bash command? -



How to get the real line number of a failing Bash command? -

in process of coming way grab errors in bash scripts, i've been experimenting "set -e", "set -e", , "trap" command. in process, i've discovered unusual behavior in how $lineno evaluated in context of functions. first, here's stripped downwards version of how i'm trying log errors:

class="lang-bash prettyprint-override">#!/bin/bash set -e trap 'echo failed on line: $lineno @ command: $bash_command && exit $?' err

now, behavior different based on failure occurs. example, if follow above with:

class="lang-bash prettyprint-override">echo "should fail at: $((lineno + 1))" false

i next output:

class="lang-none prettyprint-override">should fail at: 6 failed on line: 6 @ command: false

everything expected. line 6 line containing single command "false". if wrap failing command in function , phone call this:

class="lang-bash prettyprint-override">function failure { echo "should fail @ $((lineno + 1))" false } failure

then next output:

class="lang-none prettyprint-override">should fail @ 7 failed on line: 5 @ command: false

as can see, $bash_command contains right failing command: "false", $lineno reporting first line of "failure" function definition current command. makes no sense me. there way line number of line referenced in $bash_command?

it's possible behavior specific older versions of bash. i'm stuck on 3.2.51 time being. if behavior has changed in later releases, still nice know if there's workaround value want on 3.2.51.

edit: i'm afraid people confused because broke illustration chunks. allow me seek clarify have, i'm getting, , want.

this script:

#!/bin/bash set -e function handle_error { local retval=$? local line=$1 echo "failed @ $line: $bash_command" exit $retval } trap 'handle_error $lineno' err function fail { echo "i expect next line failing line: $((lineno + 1))" command_that_fails } fail

now, expect next output:

class="lang-none prettyprint-override">i expect next line failing line: 14 failed @ 14: command_that_fails

now, get next output:

class="lang-none prettyprint-override">i expect next line failing line: 14 failed @ 12: command_that_fails

but line 12 not command_that_fails. line 12 function fail {, less helpful. have examined ${bash_lineno[@]} array, , not have entry line 14.

for bash releases prior 4.1, special level of awful, hacky, performance-killing hell needed work around issue wherein, on errors, scheme jumps function definition point before invoking error handler.

#!/bin/bash set -e set -o functrace function handle_error { local retval=$? local line=${last_lineno:-$1} echo "failed @ $line: $bash_command" echo "trace: " "$@" exit $retval } if (( ${bash_version%%.*} <= 3 )) || [[ ${bash_version%.*} = 4.0 ]]; trap '[[ $funcname = handle_error ]] || { last_lineno=$real_lineno; real_lineno=$lineno; }' debug fi trap 'handle_error $lineno ${bash_lineno[@]}' err fail() { echo "i expect next line failing line: $((lineno + 1))" command_that_fails } fail

bash

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -