0

this is a small little script that diffs file against another folder. if there is no diff, the retrun code is zero - for success, and if there is a difference between the two files, the diff returns a 1, for unsuccessful. If you use the quiet flag the diff returns a very nice " these two files differ notice.

I guess the only problem is that i want to use the if statement, but I don't really want to print anything out on a "no diff in files" or successful. I don't really want to print the zero for success. but i cannot leave the if ; then "nothing" fi. I have to put something command like after the then. If the line is black after the "then" the program errors out. "bash if" demands a command after " then " If I just put in an echo statement, it prints our a blank line. and I do not want that either. I would like the script to do nothing on total success, and only tell me when if fails. SO I don not want the echo "$?" return the return code. actually I don't want anything there. I want the opposite of an "if success". I tried testing for a if [ ! diff original target ] but that did now work.

capser@casperhost:~$ cat /come/and/play/with_us/danny/puc_diff.sh2
#!/bin/bash
#set -x

original="/home/casper"
target="/sbcimp/dyn/data/EVT/GSD/scripts"
date=$(/bin/date +%Y%m%d)

for i in PUCZ.pl POI.pl PUConoff.pl PUCVolBands.pl PUCC.pl
do
if  diff -q $original/$i $target/$i
then
echo "$?"

fi
done


exit 0


capser@casperhost:~$ /come/and/play/with_us/danny/puc_diff.sh2
0
0
0
0
Files /home/casper/PUCC.pl and /where/on/a/road/to/nowhere/PUCC.pl differ
capser@casperhost:~$

~ ~

capser
  • 2,442
  • 5
  • 42
  • 74
  • As in `if [ ! diff -q $original/$i $target/$i ]` ? – John3136 Nov 20 '14 at 23:41
  • yeah you would figure, however when i use the above - it says "line 10: [: too many arguments " – capser Nov 20 '14 at 23:45
  • 2
    `[` doesn't take arbitrary commands. If you want to test the return status of a command (which `[` is) in an `if` statement you just use `if command; then`. So `if ! diff -q "$original/$i" "$target/$i"; then`. Remember `[` is a command. – Etan Reisner Nov 20 '14 at 23:50
  • Good point. Guess I forgot since I normally run the command and check $? rather than doing it all on one line. Here's a good answer about square brackets http://stackoverflow.com/a/8934070/857132 – John3136 Nov 21 '14 at 00:13
  • If you wan **NO** output from the diff command, just redirect output to `/dev/null` (e.g. `if ! diff -q "$original/$i" "$target/$i" &>/dev/null`) – David C. Rankin Nov 21 '14 at 01:44

1 Answers1

0

Do you want this?

function casper_diff() {
    original="/home/casper"
    target="/sbcimp/dyn/data/EVT/GSD/scripts"
    date=$(/bin/date +%Y%m%d)

    for i in PUCZ.pl POI.pl PUConoff.pl PUCVolBands.pl PUCC.pl; do
        if ! diff -q $original/$i $target/$i; then
            return 1 # difference found, return
        fi
    done
    return 0
}

casper_diff

You can use this syntax to invert the sense:

if ! diff -q $original/$i $target/$i; then
    echo "same"
fi

If you want to leave something blank, use the ":"

if diff -q $original/$i $target/$i; then
    : # nop
else
    echo "same"
fi
lx42.de
  • 418
  • 2
  • 4
  • actually the if ! ; then : fi is just what I was looking for, the bare colon prints out nothing and fulfills the needs of then statement - very good - there is nothing on the internet like it. – capser Nov 20 '14 at 23:48