-1

I want to run a command and compare the result the command produces to another value, and then either echo 0 or 1.

For example (in pseudocode) something along the lines of:

if ($(app --version) == "1.2.3") then echo 1" else echo "0"

Preferably the actual command output from app --version would be suppressed so that only 0 or 1 is written to stdout, depending on the result.

How can I do this with bash? Is there a quick one liner for that?

9
  • What have you tried so far? Where are you stuck? Commented Nov 26, 2020 at 13:02
  • @NicoHaase I tried something like $(app --version) -eq "1.2.3" && echo "0" || echo "1", but that results in the version being interpreted as a command. Also the output itsn't surpressed. I am not to familiar with the bash scripting syntax. I guess for surpressing the output of the command one could pipe to /dev/null ..? but first i gotta get the actual comparison to work Commented Nov 26, 2020 at 13:08
  • 1
    Put the result of cmd into double quotes and brackets. e.g. [ "$(app --version)" == "1.2.3" ] && echo 1 || echo 0 Commented Nov 26, 2020 at 13:09
  • @MilanDufek That results in = not found. Commented Nov 26, 2020 at 13:13
  • 1
    Is the --version flag actually supported? Commented Nov 26, 2020 at 13:55

2 Answers 2

1

Does these work?

$ if [ "$(<path>/app --version)" = "1.2.3" ]; then echo 0; else echo 1; fi
0
$ [ "$(<path>/app --version)" = "1.2.3" ] && echo 0 || echo 1
0
Sign up to request clarification or add additional context in comments.

4 Comments

No, unfortunately not. I still get the same error I described in the comments: i.imgur.com/LxTVIXo.png EDIT: Running it directly in bash makes it work. Weird, not sure why it does not work in zsh.
I didn't know you were using zsh. == is not standard, the POSIX way is =, make that change in the above comparison and it should work in zsh as well
@n00b.exe : You asked for a bash solution, so you got a bash solution. If you want one in zsh, or FORTRAN, or whatever, specify so in your question please.
@user1934428 Yea, I should have made that more clear.
1
if [[ $(app --version) == 1.2.3 ]]
then
  echo 0
else
  echo 1
fi

would work in bash (and in zsh and ksh, in case this is important).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.