LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-27-2009, 01:57 PM   #1
mgsmith7475
LQ Newbie
 
Registered: Mar 2008
Location: Michigan
Distribution: RHEL 5.4 / Ubuntu 9.10
Posts: 8

Rep: Reputation: 0
Having a problem getting my shell script to send an email after each row.


I am using grep and also cut to dissect a file that contains this sample text.

Route,# of Stops,
DistpatchTime,ReturnTime,Miles,Cases,Backhaul,Driver,Email addresss alias
364,4,02:00 Wed,11:48 Wed,94,729,0,SMITH,33229
365,4,20:30 Tue,06:33 Wed,148,1270,0,BRADNER,33277
366,4,01:30 Wed,13:10 Wed,65,1211,0,BEACH,81234

I am ignoring the header field descriptions and concentrating on each of the three lines below it.
My goal here is to extract "each" comma separated value in each row, echo it out into a body of text and send a separate email using the mail command. The last field in each row represents an email alias that I plan on appending an email domain to. Essentially each line will be a separate email going to whomever.
My script works perfectly fine if I only have one row inside the text file.

I am really new at this stuff honestly.
This is what I have thus far. Any help would be very appreciated!

#!/bin/bash -vx
dos2unix /home/chiptransfer/Route.cvs
grep -v 'Route' Route.cvs 2>&1 | cut -f1,2,3,4,5,6,7,8,9 -d',' 2>&1 | sed -e 's/Wed/Wednesday/g' -e 's/Tue/Tuesday/g' >/home/chiptransfer/Step1.txt
dos2unix /home/chiptransfer/Step1.txt
ROUTE="$(cut -f1 -d',' /home/chiptransfer/Step1.txt)"
STOPNUMBERS="$(cut -f2 -d',' /home/chiptransfer/Step1.txt)"
DISPATCHTIME="$(cut -f3 -d',' /home/chiptransfer/Step1.txt)"
RETURNTIME="$(cut -f4 -d',' /home/chiptransfer/Step1.txt)"
MILES="$(cut -f5 -d',' /home/chiptransfer/Step1.txt)"
CASES="$(cut -f6 -d',' /home/chiptransfer/Step1.txt)"
BACKHAUL="$(cut -f7 -d',' /home/chiptransfer/Step1.txt)"
DRIVER="$(cut -f8 -d',' /home/chiptransfer/Step1.txt)"
MAILBOXID="$(cut -f9 -d',' /home/chiptransfer/Step1.txt)"
SMTP=@voicmail.keyfob.com
FULLEMAIL=$MAILBOXID$SMTP

if [ "$ROUTE" != "0" ]; then
echo " Hello Mr. $DRIVER. I am here to inform you of your scheduled rowte information for rowte number $ROUTE. Your Rowte number $ROUTE consists of $STOPNUMBERS stop numbers departing at $DISPATCHTIME and returning at approximately $RETURNTIME. The number of miles for this trip equals a grand total of $MILES miles and consisting of a total case count of $CASES. Thank you and have a pleasant trip to and from your destination Mr. $DRIVER." | mail -s "Rowte Information" $FULLEMAIL
fi
done
 
Old 04-27-2009, 02:25 PM   #2
emetib
Member
 
Registered: Feb 2003
Posts: 484

Rep: Reputation: 33
i have a shell that does something similar.
sed -i 'ld' filename

i was using that line to strip the first line of the filename, and then looped it back around until it was finished with what it had to do.

hope that helps.
 
Old 04-27-2009, 06:54 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
what you need is a loop something like
Code:
for rec in `cat Route.cvs`
do
    if [[ $rec =~ "Route" ]]
    then
        continue   # skip this rec and get next one
    fi
#rest of code here
done   # end of loop
http://www.tldp.org/LDP/abs/html/loopcontrol.html

You don't need to run dos2unix more than once. Newly created files will be Linux type (assuming this is run on Linux).
TBH, for something like this I'd do it in Perl, much better for this kind of munging and emailing.
http://perldoc.perl.org/
 
Old 04-27-2009, 08:28 PM   #4
mgsmith7475
LQ Newbie
 
Registered: Mar 2008
Location: Michigan
Distribution: RHEL 5.4 / Ubuntu 9.10
Posts: 8

Original Poster
Rep: Reputation: 0
chrism01

I really appreciate the response! I will try this logic first thing tomorrow at work. Thanks again!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Shell Script to Execute IF new row in Postgres SQL is added? (PHP) gatorpower Programming 1 03-18-2009 12:45 AM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 05:03 AM
write a shell script to send email yenonn Programming 4 02-11-2005 10:06 PM
Qmail: send email from shell mikeshn Linux - General 1 09-30-2004 01:19 AM
send email through shell mikeshn Linux - General 10 01-01-2004 02:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:55 PM.

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