1

I am trying to run the diff command on two folders, check the return value and then output a message.

I have this:

#!/bin/bash

result='diff dir1 dir2'

if result == 0
then
    echo "OK"
else
    echo "ERROR"
fi

But I am getting result: command not found

How should I execute the command and return the value, to be compared in the IF?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
user997112
  • 29,025
  • 43
  • 182
  • 361
  • 1
    I'm no bash expert, but try `${result}`. – xilpex May 04 '20 at 17:55
  • You need to use $result instead of result in the if statement. – Ankush May 04 '20 at 17:55
  • Also, use back-tics ` instead of single quotes, and $? to check the return value of the command. $? is a special variable that hold the exit code of the last command executed. – Ankush May 04 '20 at 17:57
  • @Xilpex No, that won't work. See the two answers for working methods. – wjandrea May 04 '20 at 18:01
  • 1
    @Ankush No, `$result == 0` won't work. See the two answers for working methods. As well, use the new command substitution syntax `$()` instead of backticks. – wjandrea May 04 '20 at 18:04

3 Answers3

4

Quite some problems here. This is an example, but you should do more research in the future.

#!/bin/bash

result="$(diff dir1 dir2)"
ret=$?
if [[ $ret -eq 0 ]]; then
    echo "OK"
else
    echo "ERROR"
fi
exit $ret
Bayou
  • 3,293
  • 1
  • 9
  • 22
  • Thanks. Why do you need both result and ret=$? – user997112 May 04 '20 at 17:58
  • 1
    @user997112 You don't, but that's how Bayou interpreted the question. It looks like you're trying to also capture the output of the `diff` command. I interpreted it the other way - see my answer. – wjandrea May 04 '20 at 18:00
  • @wjandrea Yes I want the return code of `diff` to be stored in `result`, so I can query it in the if statement (I know I can do it your way, but my diff command has a few flags too) – user997112 May 04 '20 at 18:02
  • @user997112 You don't need to store the return code, that's what I'm trying to say in my answer. – wjandrea May 04 '20 at 18:05
  • @user997112 Also to be clear, "output" and "return code" are not the same thing. – wjandrea May 04 '20 at 18:06
  • @wjandrea I want the return code, not the output. To clarify, I want to store the return code to a variable because I didn't want to put a long command in an IF statement – user997112 May 04 '20 at 18:13
  • The return value is not returned by the program this way. You always should use `$?` for that. – Bayou May 04 '20 at 18:18
  • @user997112 FWIW you can avoid long command lines by putting the arguments in an array – wjandrea May 04 '20 at 18:51
3

The simplest way is to check it directly:

#!/bin/bash

if diff dir1 dir2; then
    echo "OK"
else
    echo "ERROR"
fi

If you don't want diff to print anything, use the -q flag.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

please use ${result} instead. Also, note the backticks on the actual command

#!/bin/bash

result=`diff dir1 dir2`

if [[ -z ${result} ]]
then
    echo "OK"
else
    echo "ERROR"
fi