Help with writing simple bash script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Help with writing simple bash script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-20-2010
sallyanne sallyanne is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 23 February 2010, 6:23 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Error Help with writing simple bash script

I want to write a bash script to:

1. Send an email from localhost to an external gmail account. (gmail then automatically forwards the message back to a pop account on the same server.

2. Script waits 3 minutes then checks to see if the email arrived, and if not, it sends an email to administrator.

Can anyone point me in the right direction? My knowledge of writing scripts is limited and I'm hoping someone can help.

Thanks so much,

Sallyanne
Sponsored Links
    #2  
Old 02-20-2010
jgt's Avatar
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 1 December 2014, 3:41 PM EST
Location: 44.21.48N 80.50.15W
Posts: 1,564
Thanks: 1
Thanked 160 Times in 152 Posts
Put some unique string into the email, and write a script with a loop in it that sleeps for 10 seconds, The script should check the recipients mail box using grep for the unique string.

Code:
#!/bin/ksh
i=1
while [ $i -lt 18 ]
do
grep mystring /var/spool/mail/mailbox
if [ $? -eq 0 ]
then
echo found 
exit 0
else
echo not found
fi
i=`expr $i + 1`
sleep 10
done
mail -s "not received" admin <<EOF
email not recieved in 3 minutes
EOF

Sponsored Links
    #3  
Old 02-21-2010
sallyanne sallyanne is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 23 February 2010, 6:23 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your advice. I'm a little confused as to how to specify the sending of the email in the first place. I can see in your example the sending of email if the recipient does not receive the email, but how to specify the sending of the first email with the unique string?

Sorry if this seems obvious, I'm new and just getting my head around things.

Appreciate any help you can offer.

Regards,
Sallyanne
    #4  
Old 02-21-2010
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 25 November 2014, 5:09 PM EST
Posts: 6,396
Thanks: 287
Thanked 673 Times in 642 Posts
I may not be able to give the final answer but "jgt" has given the key to the solution and I may be able to help others by asking questions.

Please state what Operating System you have and what mail client you are running?

Can you send a mail by using say "mail" or "mailx" from the command line? If so, what do you type? In general if you can type it at the command line, it can be scripted.
Sponsored Links
    #5  
Old 02-21-2010
jgt's Avatar
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 1 December 2014, 3:41 PM EST
Location: 44.21.48N 80.50.15W
Posts: 1,564
Thanks: 1
Thanked 160 Times in 152 Posts
To send an email, with a unique string (provided not more than one email per second is generated.)



Code:
#set UNIQUE to Process ID + current date and time
UNIQUE=$$`date +"%Y%m%d%H%M%S`
echo $UNIQUE  >>output.log
mail -s "Test Response Time" someone@gmail.com <<EOF
Remarks:$UNIQUE
Sent at `date`
EOF

Sponsored Links
    #6  
Old 02-22-2010
sallyanne sallyanne is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 23 February 2010, 6:23 AM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
I am running Redhat Enterprise 5 and running gmail as the MTA. Yes, I can send an email via the command line using the mail command.

The real problem I'm having now is I really don't know how to tie all the bits and pieces together which I need to make this email monitoring script work

I know it's a big ask (maybe too big) for someone to provide more specific and final code which I can use.

Thanking you all for your advice and assistance. It's a steep learning curve for me!

Regards,
Sallyanne
Sponsored Links
    #7  
Old 02-22-2010
jgt's Avatar
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 1 December 2014, 3:41 PM EST
Location: 44.21.48N 80.50.15W
Posts: 1,564
Thanks: 1
Thanked 160 Times in 152 Posts
Assuming that you want to run this test on demand, just join the two scripts together as:


Code:
#!/bin/ksh
#set UNIQUE to Process ID + current date and time
UNIQUE=$$`date +"%Y%m%d%H%M%S`
echo $UNIQUE  >>output.log
mail -s "Test Response Time" someone@gmail.com <<EOF
Remarks:$UNIQUE
Sent at `date`
EOF
#test to see if received
i=1
while [ $i -lt 18 ]
do
grep $UNIQUE /var/spool/mail/mailbox
if [ $? -eq 0 ]
then
echo Received 
echo $UNIQUE `date` >>output.log
exit 0
else
echo Not yet
fi
i=`expr $i + 1`
sleep 10
done
mail -s "not received" admin <<EOF
email not recieved in 3 minutes
EOF


Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
HELP! writing simple shell script deadleg UNIX for Dummies Questions & Answers 3 03-11-2008 09:30 PM
Writing Bash script boris Shell Programming and Scripting 1 04-22-2007 06:45 AM
Problems writing bash script to unzip files siegfried Shell Programming and Scripting 1 09-18-2006 03:27 PM
Simple Bash Script xaphalanx Shell Programming and Scripting 3 12-21-2005 03:54 PM
Need help writing simple script tamdoankc UNIX for Dummies Questions & Answers 1 08-08-2001 08:48 PM



All times are GMT -4. The time now is 12:02 AM.