bash - Wrapper script to exit main script with debug -
i have bash script runs several commands in order , stops if 1 returns non 0 error code , displays line number locate command.
after each command run function (exitiflastcommandreturncodenotzero) checks if exit code ok , if not displays line number , error code. example:
.. cmd1 param1 param2 ; exitiflastcommandreturncodenotzero $? $lineno cmd2 param1 param2 ; exitiflastcommandreturncodenotzero $? $lineno cmd3 param1 param2 ; exitiflastcommandreturncodenotzero $? $lineno ..
this works there built-in function or script can wrap commands , give me same functionality? example:
.. wrapperscript cmd1 param1 param2 wrapperscript cmd2 param1 param2 wrapperscript cmd3 param1 param2 ..
or better 'block' level function runs commands , exits if 1 command fails. example:
wrapperscript_pseudocode { .. cmd1 param1 param2 cmd2 param1 param2 cmd3 param1 param2 .. }
ideally, output when command fails should include (apart line number) command name , parameters.
use trap. can turn trap on , off @ various places in code if like.
#!/bin/bash function error { local err=$? echo "error# $err on line $bash_lineno: $bash_command" exit $err } trap error err # commands ... echo bar /bin/false foo # testing echo baz
exitiflastcommandreturncodenotzero
seriously?
Comments
Post a Comment