Does bash have a boolean and / or statement that does not have a short circuit?

I am looking to always execute both validateA and validateB:

function validateA() { echo "A [ok]" return 0 } function validateB() { echo "B [ok]" return 0 } if ! validateA | ! validateB; then echo "validation [fail]" exit 1 else echo "validation [ok]" exit 0 fi 
+4
source share
3 answers

You can simply call them independently and capture the return values:

 function validateA() { echo "A [fail]" return 1 } function validateB() { echo "B [ok]" return 0 } validateA ; vA=$? validateB ; vB=$? if [[ $vA -ne 0 || $vB -ne 0 ]] ; then echo "validation [fail]" exit 1 else echo "validation [ok]" exit 0 fi 

It is output:

 A [fail] B [ok] validation [fail] 
+4
source

I had the same question and I did something like this:

 rc=0 if ! validateA ; then rc=1; fi if ! validateB ; then rc=1; fi return $rc 

This is the same principle as the other answers, but more concise syntax.

+2
source

The first idea that comes to my mind is to simply call each function sequentially, save the return values, and analyze them later. Like this:

 validateA a_retval=$? validateB b_retval=$? if [ $a_retval -ne 0 -o $b_retval -ne 0 ]; then echo "validation [fail]" exit 1 else echo "validation [ok]" exit 0 fi 
+1
source

Source: https://habr.com/ru/post/1381075/


All Articles