Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does bash have a boolean and/or operator that doesn't short circuit?

Tags:

bash

I'm looking to be able 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
like image 461
chickeninabiscuit Avatar asked Nov 14 '11 06:11

chickeninabiscuit


2 Answers

You can just call them regardless 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

This outputs:

A [fail]
B [ok]
validation [fail]
like image 152
paxdiablo Avatar answered Sep 18 '22 03:09

paxdiablo


I had the same question and ended up doing something like this:

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

It's the same principle as other answers but a more condensed syntax.

like image 40
PaulC Avatar answered Sep 19 '22 03:09

PaulC