-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?

n00b.exe
  • 287
  • 1
  • 3
  • 12
  • What have you tried so far? Where are you stuck? – Nico Haase Nov 26 '20 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 – n00b.exe Nov 26 '20 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` – Milan Dufek Nov 26 '20 at 13:09
  • @MilanDufek That results in `= not found`. – n00b.exe Nov 26 '20 at 13:13
  • 1
    Is the --version flag actually supported? – Raman Sailopal Nov 26 '20 at 13:55
  • @n00b.exe It should never results in this. Even if `app` doesn't exists it results in STDERR and then returns 0. Try it step by step. Or start in new shell `bash -x` – Milan Dufek Nov 26 '20 at 14:25
  • It works in bash, in zsh I get thrown the error I mentioned above. Thats really weird. – n00b.exe Nov 26 '20 at 14:30
  • @tripleee It is not a duplicate. This question expands on the problem by requiring output supression and direct command output comparison. Not just comparing two values. – n00b.exe Nov 26 '20 at 17:03
  • Command substitution `$(...)` already suppresses standard output. You used that syntax so we assume you know what it actually does. – tripleee Nov 26 '20 at 17:05

2 Answers2

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
imthor
  • 563
  • 3
  • 6
  • No, unfortunately not. I still get the same error I described in the comments: https://i.imgur.com/LxTVIXo.png EDIT: Running it directly in bash makes it work. Weird, not sure why it does not work in zsh. – n00b.exe Nov 26 '20 at 14:29
  • 1
    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 – imthor Nov 26 '20 at 14:52
  • 1
    @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 Nov 26 '20 at 15:28
  • @user1934428 Yea, I should have made that more clear. – n00b.exe Nov 26 '20 at 17:03
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).

user1934428
  • 19,864
  • 7
  • 42
  • 87