0

I am having trouble getting a bash if statement to only execute if three functions all return zero. I made a simple example and could not get the if statement at the bottom to run (the else portion of the code runs). Am I setting up the if statement wrong?

test() {
  NUM=3
  BUM=4
  if [ $(NUM) -lt $(BUM) ];
  then 
    return 0;
  else
    return 1;
  fi;
}

test2() {
  NUM=4
  BUM=5
  if [ $(NUM) -lt $(BUM) ];
  then 
    return 0;
  else
    return 1;
  fi;
}

test3() {
  NUM=13
  BUM=133
  if [ $(NUM) -lt $(BUM) ];
  then 
    return 0;
  else
    return 1;
  fi;
}

if [[ ${test} && ${test2} && ${test3} ]]; then 
  echo "successfully chained function values"
else
  echo "ands did not chain successfully"
fi

1 Answer 1

5

Don't use [[ , it tests conditions but doesn't run anything. Use the functions directly:

if test && test2 && test3 ; then
    echo Success
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.