3

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,

1
  • Is what you say what you want in the case that suid.new exists, but suid.old does not? And if neither suid.old and suid.new both exist, do you really want to run the same find (modulo the apparently unintended 4000/-4000) twice? Feb 8, 2010 at 14:39

3 Answers 3

1

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."

4
  • thanks, it worked, is there a way to add another command to say run this every night. * 24 * * * meaning it will run at 12 every night. But i don't know how do i include this in the script. Thanks,
    – su
    Feb 8, 2010 at 3:57
  • @su: Edit your crontab using crontab -e and add the line * 0 * * * /path/to/your/script Feb 8, 2010 at 5:37
  • 2
    Umm, if suid.old and suid.new exist then your script always prints the same thing based on whatever are in suid.old and suid.new without updating those files. Don't you probably want to replace the suid.new file with new data if suid.old exists so you get updated results? Maybe after you diff you should mv suid.new suid.old ?
    – Paul
    Feb 8, 2010 at 8:08
  • @~drpaulbrewer: You are correct. Thanks for the suggestion. I've updated my answer. Feb 8, 2010 at 10:58
0

Remove the 'ls' from your find lines. They should look like find / -perm -4000 -0 -perm -2000 > suid.old

the way you have it setup find thinks that ls is a path argument. Since find prints the results to STDOUT, just doing a normal redirect will get the output you want.

2
  • Hi, i removed the -ls from the find. But i still get an error: diff: suid.old: No such file or directory. The script was suppose to create one if it does not exist. the "suid.old one"
    – su
    Feb 8, 2010 at 2:18
  • 1
    @su: run just your find in a terminal. and see what error it is giving, also see dennis williamson's answer about the leading slash
    – Zypher
    Feb 8, 2010 at 3:01
0

My version:

#!/bin/bash

if [ ! -f "/suid.old" ]; then                          # note: !
   find / -perm -4000 -o -perm -2000 -ls > /suid.old   # note: dash before ls; slash before file
else
   find / -perm 4000 -o -perm -2000 -ls > /suid.new
   diff /suid.old /suid.new > /newchanges.list
   mv /suid.new /suid.old                              # could be handy
fi

You must log in to answer this question.