I am looking for a way to print line numbers inside the line script during errors of errors.
I came to the '-x' option, which prints the line while running the shell script, but this is not what I need. Maybe I can $ LINENO before each exit code? Is there a cleaner way to do this?
I just wanted the line number so that I could open the shell script and go straight to the place where the interpreter realized the error. Using
PS4 = ': $ LINENO +'
will add line numbers to the output of set -x
.
If you want to print on only errors, then there is some risk to it being running in insects among recent interpreters, however, You can try:
error () {local parent_lineno = "$ 1" local message = "$ 2" local code = "$ {3: -1}" if [[- N "$ message"]]; Then "$ {parent_lineno}: $ {parent_lineno} on line $ {parent_lineno}: resonates error; status goes with $ $ {code}" "second echo" error; $ {parent_linen}; $$ {code} "ex exit" $ {Code} "} 'error $ {LINENO}' ERR
Again, this will not work On the construction of some bass, which does not always set correctly inside the lineno
trap.
Another approach (which only Will work on recent shells; 4.1 attributes) Each code has to use the PS4
to use the exit status and line number for a dedicated file descriptor, and only use it To print the line, use tail
before shell removal FD:
exec {BASH_XTRACEFD}> gt; & gt; (tail-N1) # Set set-x output tail- n 1 ps4 = 'line $ LINENO; pre-order exit status $? +' Set-x
No comments:
Post a Comment