Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

In bash, can you use a function call as a condition in an if statement?

here's what i'm trying to achive:

function f1() {
  return 0
}

function f2() {
  return 0
}

if [[ f1 && f2 ]]; then
  echo "success"
else
  echo "fail"
fi
question from:https://stackoverflow.com/questions/8117822/in-bash-can-you-use-a-function-call-as-a-condition-in-an-if-statement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't use [[ (or [) when running a command and checking the result code.

if f1 && f2 ; then
  echo "success"
else
  echo "fail"
fi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...