Shell script using Diff

Hello - I have a small diff script that checks 2 directories. It reports the difference in count such as wc -l, and also names the different files.

How should I get "ERROR: diff found . (host)" - when it actually finds a diff?

This is how I have written:

#!/bin/bash

            DIFF=$(diff -r /home/student/bin/dir1 /home/student/bin/dir2 | grep Only | wc -l)
            DIFF2=$(diff -r /home/student/bin/dir1 /home/student/bin/dir2 | grep Only)
                        echo "The file count difference between master folder and CE-B-CM\D folder is:
${DIFF}"
            
            
            echo "The names of the different files and their paths are:
${DIFF2}."

            echo "####################################################"
#!/usr/local/bin/bash

DIFF=$(diff -r /path/to/dir1 /path/to/dir2)

if [[ $DIFF != "" ]]; then
echo "ERROR: diff found. $HOSTNAME"
fi

That is only giving me my own local host name. Not the path to the host where the difference is found and the count...

i use this script, however, I am not able to flag the email subject line to either ERROR or GOOD, have a look:

#!/bin/bash
# Diff script to be used for file comparisions

DIFF=$(diff -r /home/student/bin/dir1 /home/student/bin/dir2 | wc -l) 
if [ $DIFF -ne 0 ]
then
echo "ERROR: Diff found a diff in total count of this many files: ${DIFF}."
forproblemmail=1
else
echo "No Difference"
fi

DIFF2=$(diff -r /home/student/bin/dir1 /home/student/bin/dir2 | grep Only)
if [ $? -eq 0 ]
then
echo "The names of the different files and their paths are:
${DIFF2}."
forproblemmail=1
else 
echo "No files are different"
fi

#Email Module
if [ $forproblemmail - eq 1 ] ; then
       SUBJECT="ERROR: Problem During Diff"
         else
              SUBJECT="GOOD: Complete Find Files With Success"
fi
# Email To ?
EMAIL="admin@somewhere.com"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
# send an email using /bin/mail
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

Is the the space between the "-" and "eq" a typo?

if [ $forproblemmail - eq 1 ] ; then

Yes! you are right.

Not clear if your problem is solved now.:slight_smile:

Was it a typo in your original script?

Actually I am not sure if the way I flagged the error by "forproblemmail=1" , would really flag for the subject line of the email?

Also I am getting this when I run it:

line 24: [: -eq: unary operator expected

Try to echo the variable $forproblemmail before the line with the error or run the script in debug mode.
Add the this line below the shebang:

set -x

Here is what I got, after i added "set -x" :


-bash-3.2$ ./diff_check.sh
++ diff -r /loc1/dir1 /loc2/dir2
++ wc -l
+ DIFF=0
+ '[' 0 -ne 0 ']'
+ echo 'No Difference'
No Difference
++ diff -r /loc1/dir1 /loc2/dir2
++ grep Only
+ DIFF2=
+ '[' 1 -eq 0 ']'
+ echo 'No files to display, as diff check is good'
No files to display, as diff check is good
+ '[' -eq 1 ']'
./diff_check.sh: line 25: [: -eq: unary operator expected
+ SUBJECT='GOOD: Diff Success'
+ EMAIL=admin@somewhere.com
+ EMAILMESSAGE=/tmp/diffmessage.txt
+ mail -s 'GOOD: Diff Success' admin@somewhere.com
Null message body; hope that's ok

Assign a value to the variable forproblemmail in the else part:

if [ $? -eq 0 ]
then
  echo "The names of the different files and their paths are:
  ${DIFF2}."
  forproblemmail=1
else 
  echo "No files are different"
  if [ -z $forproblemmail ]; then
    forproblemmail=0
  fi
fi

Thanks - il try it