Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need a shell script which checks if file exist or not. If file exists then send an email and if file does not exist it execute the script after sometime.
Sorry, but please understand that LQ is not here to be an "on call" resource where you order up scripts like you would order a burger at a fast food restaurant.
Read up on the information threads about LQ and you'll learn that the intentions are to "help" but really to help you learn to accomplish these things yourself.
I'm rather surprised that you haven't taken the time to read up on LQ much in the past 3 years since you signed up for an account. At least one of your posted threads a few weeks ago shows you having used a shell script and asked some questions about it.
Therefore you can use some of the links in my signature to read up on BASH shell scripting, there are a variety of guides offered. There's also a link to the LQ guide on how to ask effective questions.
Notes also that checking whether or not a file exists is a very simple BASH script action and performing a simple web search can at least guide you in that part of your script.
If/When you have a first attempted script, why not post it in this thread and people can help you progress further with it. Remember to use [code][/code] tags to enclose any code or scripts you post.
I need a shell script which checks if file exist or not. If file exists then send an email and if file does not exist it execute the script after sometime.
I created a shell script named test.sh
Can I define time interval to execute same script after sometime. For example after 5 minutes
I am talking about line 6 to execute script test.sh again after some time.
#!/bin/bash
FILE=$1
if [ -f $FILE ];
then
echo "File $FILE exists"
else
test.sh new.txt
fi
I created a shell script named test.sh
Can I define time interval to execute same script after sometime. For example after 5 minutes
I am talking about line 6 to execute script test.sh again after some time.
Before we get into to that, I have to ask if you have even tried to execute this program?
Because if you have, you would have noticed that it does not do what you want it to.
Code:
#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
echo "File $FILE exists"
else
test.sh new.txt
fi
Testrun without argument(s):
Code:
./test.sh
File exists
Testrun with argument:
Code:
./test.sh ~/foobar
./test.sh: line 7: test.sh: command not found
Good start. Please use [code][/code] tags to enclose your code. There's a link in my signature which describes how to do that.
People will help you to refine the script, so give some more details on the next step of results you wish to attain.
Note as HMW is pointing out that you are ignoring the case where there is not an argument. The variable $# gives you the number of arguments passed, so you can test that to make sure it's exactly 1.
What do you next want to do with this script?
Do you want to execute some action every 5 minutes? There's a "sleep" command which will wait the specified time you give to it.
Run it at a certain time of day, have a delay and then send the email? You can schedule the script to run with a cron job so that it will run at a given time of day.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.