开发者

if then else statement to find files

Hello how can i use the if/else statement to look for files. Lets say if i have a file.txt in开发者_如何学JAVA 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.


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


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.


Something like this should work:

if test -e "file.txt"; then
  cat file.txt | mail -s "file.txt changed" example@example.com
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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜