tags:

views:

79

answers:

3

Hello how can i use the if/else statement to look for files. Lets say if i have a file.txt in root, i want to be able to write a scrip that says if [ file.txt ] does not exist then use the find / -newer sshh > file.txt and or else use [file.txt] and then mail the changes if anything has changed.

+1  A: 

In bash, -f will tell you if a file exists.

if [ -f /file.txt ];
  then
    // do something with mail
  else
    // do something with find
fi
danben
fi? mean. at the end?
su
Your `test` syntax is bogus.
Ignacio Vazquez-Abrams
Sorry, left out the spaces inside the brackets.
danben
@su - `fi` closes `if`.
danben
A: 

You didnt really tell us a lot about your platform, but IMHO - if you don't know how to do something like this off the top of your head with find and basic shell builtins - you should probably use a higher level language. It will be easier to conceptualize and easier to maintain.

Take it as a learning experience - a change to experiment with python, perl or a language of your choice.

Shane C. Mason
A: 

Something like this should work:

if test -e "file.txt"; then
  cat file.txt | mail -s "file.txt changed" [email protected]
else
  find / -newer sshh > file.txt
fi

But you most likely want to combine it with diff to find out if the file structure changed.

Johan Dahlin
You get a “Useless Use of Cat” award for that.
Chris Johnsen
Thanks, if it's an award it must be good :-)
Johan Dahlin