tags:

views:

53

answers:

3

Hello,

I am writing a simple Bash script to detect when a folder has been modified.

It is something very close to:

ls -lR $dir > a
ls -lR $dir > b

DIFF=$(diff a b) 
if [ $DIFF -ne 0 ] 
then
    echo "The directory was modified"

Unfortunately, the if statement prints an error: [: -ne: unary operator expected

I am not sure what is wrong with my script, would anyone please be able to help me?

Thank you very much!

Jary

+3  A: 

You are looking for the return value of diff and not the output of diff that you are using in your example code.

Try this:

diff a b
if[ $? -ne 0 ]
then
    echo "The directory was modified"
tangens
Thank you very much! This works perfectly!
Jary
+4  A: 
if ! diff -q a b > /dev/null ; then
  echo "they differ"
fi
Paul Tomblin
This is the most direct way of doing the test. (Note: you should add a close quote to the echo command.)
Gordon Davisson
@Gordon, sorry, cut and paste error. Fixed now.
Paul Tomblin
+2  A: 
ls -lR $dir > a
ls -lR $dir > b

DIFF=$(diff a b) 
if [ "$DIFF" != "" ] 
then
    echo "The directory was modified"
fi
Lou Franco
Thanks, it seems to work well as well, thanks!
Jary