LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 07-07-2015, 06:43 AM   #1
starqazi
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Rep: Reputation: Disabled
Shell script required


Hi All,

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.

needing your help.

Regards,
Qazi
 
Old 07-07-2015, 06:51 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
What have you done so far?
 
1 members found this post helpful.
Old 07-07-2015, 06:52 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
And do you want a side order of fries with that?

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.

Last edited by rtmistler; 07-07-2015 at 06:53 AM.
 
1 members found this post helpful.
Old 07-07-2015, 09:05 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by starqazi View Post
Hi All,

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.

needing your help.

Regards,
Qazi
Read the bash manpage.
 
Old 07-07-2015, 09:40 AM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
Read the bash manpage.
Code:
man bash | less +/^"CONDITIONAL EXPRESSIONS"
to be exact.

Go and sin no more.
 
Old 07-08-2015, 01:17 AM   #6
starqazi
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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


Regards,
Qazi
 
Old 07-08-2015, 01:47 AM   #7
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by starqazi View Post
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
Please read what rtmistler posted, and read more here.

Best regards,
HMW

Last edited by HMW; 07-08-2015 at 01:49 AM. Reason: Added link
 
Old 07-08-2015, 04:53 AM   #8
starqazi
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
I got a solution by using "at" command in my script.




#!/bin/bash
FILE=$1

if [ -f $FILE ];
then
echo "File $FILE exists"
else
at 23:40 <<< 'test.sh new.txt'
fi



***************
If script cannot find the file new.txt at first run then it execute the script again at time defined by "at" command.

Thanks all who helped me.

Regards,
Qazi
 
Old 07-08-2015, 06:51 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
1 members found this post helpful.
Old 07-08-2015, 06:27 PM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
And for the email part:

How can i send an email through UNIX mailx command
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script required to change directories in linux kavan.shah Linux - Newbie 12 05-19-2014 01:52 PM
[SOLVED] Shell script for recursive delete required. bikbjrt Linux - Newbie 2 07-01-2010 12:25 AM
Shell script assistance required.. TheEngineer Linux - Newbie 4 10-26-2006 06:42 AM
shell script help required syntax error sridhar11 Programming 1 10-31-2005 08:38 AM
small shell script required demigor Programming 6 08-08-2005 04:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration