ANDing exit codes in bash -


i have bash script runs 3 checks on source code, , exit 0 if commands succeeded, or exit 1 if of them failed:

#!/bin/bash  test1 ./src/ --test-1=option exit_1=$?  test2 ./src/ test-2-options exit_2=$?  test3 ./src/ -t 3 -o options exit_3=$?  # exit error if of above failed [[ $exit_1 -eq 0 && $exit_2 -eq 0 && $exit_3 -eq 0 ]] exit $? 

this code works, feels overly long , verbose. there way can made nicer? not happy with:

  • having run command, , then assign exit code variable
  • having use [[ ... ]], then collect exit code on next line exit with
  • having explicitly compare variables 0, in [[ $var -eq 0 ]], instead of treating them booleans

ideally, end result more readable like:

exit_1=( test1 ./src/ --test-1=option ) exit_2=( test2 ./src/ test-2-options ) exit_3=( test3 ./src/ -t 3 -o options )  # exit error if of above failed exit ( $exit_1 && $exit_2 && $exit_3 ) 

some things have considered:


getting error code in variable in 1 line:

exit_1=$( test1 ./src/ --test-1=option )$? exit_2=$( test2 ./src/ test-2-options )$? exit_3=$( test3 ./src/ -t 3 -o options )$? 

this works, , makes bit shorter, i've never seen else use before. sensible/sane thing do? there issues this?


just running tests, , && them together:

test1 ./src/ --test-1=option && \ test2 ./src/ test-2-options && \ test3 ./src/ -t 3 -o options status=$? 

this not work, bash short circuits. if test1 fails, test2 , test3 not run, , want them run.


detecing errors , exiting using || exit

[[ $exit_1 -eq 0 && $exit_2 -eq 0 && $exit_3 -eq 0 ]] || exit 1 

this saves 1 line of awkward exit codes , variables, important bit of exit 1 right @ end of line can miss it. ideally, work:

exit [[ $exit_1 -eq 0 && $exit_2 -eq 0 && $exit_3 -eq 0 ]] 

of course, not work, [[ returns output instead of echoing it.

exit $( [[ $exit_1 -eq 0 && $exit_2 -eq 0 && $exit_3 -eq 0 ]] ; echo $? ) 

does work, still seems horrid cludge


not explicitly dealing exit-codes-as-boolean

[[ $exit_1 && $exit_2 && $exit_3 ]] 

this not hope do. easiest way of && 3 return codes stored in variables full $var -eq 0 && .... surely there nicer way?


i know bash not nice programming language - if can call - there way can make less awkward?

you can use bash's arithmetic command or exit codes together, , negate result, exit code of 1 if of codes non-zero. first, example:

$ ! (( 0 | 0 | 0 )); echo $? 0 $ ! (( 1 | 0 | 0 )); echo $? 1 

now, script:

#!/bin/bash  test1 ./src/ --test-1=option; exit_1=$? test2 ./src/ test-2-options;  exit_2=$?    test3 ./src/ -t 3 -o options; exit_3=$?  # exit error if of above failed. no need final # call exit, if last command in script ! (( $exit_1 || $exit_2 || $exit_3 )) 

or in general, can accumulate exit codes run arbitrary number of tests:

#!/bin/bash  # unfortunately, ||= not assignment operator in bash. # use |=, suppose; may not able assign # meaning particular non-zero value, though. test1 ./src/ --test-1=option; (( exit_status = exit_status || $? )) test2 ./src/ test-2-options;  (( exit_status = exit_status || $? ))   test3 ./src/ -t 3 -o options; (( exit_status = exit_status || $? )) # ... testn ./src "${final_option_list[@]}"; (( exit_status = exit_status || $? ))  exit $exit_status   # 0 if succeeded, 1 if failed 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -