[SlugBug] shell script help

Chris J cej at nightwolf.org.uk
Thu Aug 7 13:16:02 BST 2003


. o O ( I've done it again; sorry Bill -- you'll get two copies )

---------------------------- Original Message ----------------------------
Subject: Re: [SlugBug] shell script help
From:    "Chris J" <cej at nightwolf.org.uk>
Date:    Thu, August 7, 2003 12:13
To:      "Bill Best" <bill at commedia.org.uk>
--------------------------------------------------------------------------


And Lo! The Great Prophet " Bill Best" uttered these words of wisdom:
>
> Many thanks for this, Daniel.  Before running this script, I amended it
just to check it, so it now looks like:
>
> for FILE in $(ls directory1/*); do
>   for FILE2 in $(ls directory2/*); do
>     if [ $(cmp $FILE $FILE2) -eq 0 ]; then            #problem here
>        echo $FILE $FILE2
>     fi
>   done
> done
>
> but it bombs out with:
>                                          [: too many arguments
> for the line indicated above.
>
> Anyone got any ideas?
>


cmp prints out stuff when it finds a difference, so the $(cmp ... ) gets
expanded to:

        file1 file2 differ: char XX, line XX

so you need to invoke cmp with "-s", which says "run silently".

Btw, I couldn't get it to work; first it didn't like the "-eq 0" and then,
if taking that out, it always returned false.

So, if you have problems, try splitting the "if [ $(cmp ... ]; then" line
as follows:

        cmp -s $FILE1 $FILE2
        if [ $? -eq 0 ]; then
            .
            .
            .

Which seems to work okay. Or to cut the "if...fi" block to a single line:

        [ $? -eq 0 ] && echo $FILE1 $FILE2

Chris...



More information about the SlugBug mailing list