Bash – help with bash script using find and diff command

bashdiff()find

i have a bash script that i need help with:

#!/bin/bash
if [ -f "/suid.old" ]
then
find / -perm -4000 -o -perm -2000 ls > suid.old
else
find / -perm 4000 -o -perm -2000 ls > suid.new

diff suid.old suid.new > newchanges.list
fi

when i run it it gives me an error saying: diff: suid.old: No such file or directory.

My script should say, if suid.old does not exist, then use the find command to create one, or else use find command to do whatever it needs to with the suid.new. after find any changes it made and redirect it to newchanges.list

please help,

Best Answer

Remove the slash from the filename in the if statement. The way you have it, it's checking for the file in the root directory, but later it's created in whatever is the current directory.

Also, your script basically says "if suid.old doesn't exist then do a diff".

You might want something like:

#!/bin/bash
if [ ! -f "suid.old" ]
then
    find / -perm -4000 -o -perm -2000 ls > suid.old
fi

if [ ! -f "suid.new" ]
then
    find / -perm 4000 -o -perm -2000 ls > suid.new
fi

diff suid.old suid.new > newchanges.list
mv suid.new suid.old

This says: "If suid.old doesn't exist, create it. If suid.new doesn't exist, create it. Now that they've been created (or already exist) do the diff."