开发者

if then else statement to find files

开发者 https://www.devze.com 2022-12-19 03:28 出处:网络
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 us

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

精彩评论

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