notmyidea
notmyidea

Reputation: 31

BASH conditional if statement ANDing two commands that output true or false

In BASH, I have an if statement like so:

if dokku ps:report $APP_NAME --deployed && ! dokku certs:report $APP_NAME --ssl-enabled; then echo "works"; fi

Each command outputs true or false. I want the then clause to get executed when the first command outputs true and the second outputs false (which is why I have the !). But when I run this, it outputs:

true
false

If I run the second command individually:

dokku certs:report $APP_NAME --ssl-enabled

It outputs false. So the negation in my original if statement isn't working. What am I doing wrong here?

Upvotes: 0

Views: 102

Answers (1)

that other guy
that other guy

Reputation: 123690

Here's an easier way to reproduce your problem:

if echo false
then
  echo "Why does this print?"
fi

This is because the command output is irrelevant. if, && and || all look at the command's exit code. They don't try to interpret English text.

You can instead use string comparison:

if [ "$(dokku ps:report $APP_NAME --deployed)" = "true" ] && [ "$(dokku certs:report $APP_NAME --ssl-enabled)" = "false" ]
then ...

Upvotes: 2

Related Questions