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
ls -lR $dir > a
ls -lR $dir > b
DIFF=$(diff a b)
if [ "$DIFF" != "" ]
then
echo "The directory was modified"
fi
if ! diff -q a b &>/dev/null; then
>&2 echo "different"
fi
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";
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With