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.
views:
79answers:
3
+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
2010-02-06 04:41:47
fi? mean. at the end?
su
2010-02-06 04:57:40
Your `test` syntax is bogus.
Ignacio Vazquez-Abrams
2010-02-06 05:03:52
Sorry, left out the spaces inside the brackets.
danben
2010-02-06 14:50:03
@su - `fi` closes `if`.
danben
2010-02-06 15:26:11
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
2010-02-06 04:42:26
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
2010-02-06 14:54:56