The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to search for text in a file and copy file imeadows UNIX for Dummies Questions & Answers 9 1 Week Ago 06:12 PM
script to search a file dr46014 Shell Programming and Scripting 1 02-28-2008 03:55 AM
Passing data from file to variables for script screwed718 Shell Programming and Scripting 2 02-14-2008 04:12 AM
File search variables bbbngowc Shell Programming and Scripting 19 01-23-2008 08:50 AM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 07:17 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 10-03-2007
Registered User
 

Join Date: Oct 2007
Posts: 11
Ksh script - Design ? - Search file and set variables

Hi -

I'm trying to think of a clever way to write a shell script (trying to stay w/ ksh as that's what I know the best...) that will resolve the following problem:

Problem - On a daily basis I have to email folks who are on-call to remind them. I was hoping to script this out so I could have a file w/ a pattern similiar to:

fileX -
Oct 3:jerry:mark
Oct 4:tim:stan

fileY - jerry:jerry@foo.com
mark:mark@foo.com
tim:tim@foo.com
peter:stan@foo.com

My first idea is to have the script
Set a varaible for the date using something like - 'date |nawk '{print $2 &3}'
Grep fileX searching for date variable - then set variableA with the first name and variableB with the second name for that date - not sure how to do this
Then take variableA and look for a match in fileY - This will set another varialbe - variableZ
Finally, run a mailto command using varialbe Z

Take this script and add it to cron to run daily.

If it's easier, rather than using their names in the file, I can set it up w/ their email addresses - that shouldn't be too much extra work to do.

Any suggestions are greatly appreciated.

Regards,
littlefrog
Reply With Quote
Forum Sponsor
  #2  
Old 10-03-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,855
Code:
IFS=':
'
set -- $(grep "$(date +%b%e)" fileX)
set -- $(egrep "$2|$3" fileY)
mailx -s "You\'re oncall today $(date +%D)" "$2 $4"
Reply With Quote
  #3  
Old 10-04-2007
Registered User
 

Join Date: Oct 2007
Posts: 11
I may be missing something.. well.. I'm sure I'm missing something.

I tried the suggestion above and tried several combinations, but I don't seem to be capturing the variables correctly. Can you take a look and see if there is anything obvious. - Thanks


>cat dtscript.ksh
#!/bin/ksh

IFS=`:
`
set -- $(grep "$(date +%b%e)" fileX)
set -- $(egrep "$2|$3" fileY)
echo "You are oncall today $(date +%D)" "$2 $4"
#mailx -s "You're oncall today $(date +%D)" "$2 $4"

>cat fileX
10/04/07:jerry:greg

>cat fileY
jerry:jerry@foo.com
greg:grep@foo.com

>./dtscript.ksh
You are oncall today 10/04/07
Reply With Quote
  #4  
Old 10-04-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,855
Set IFS with single (not back) quotes:

Code:
zsh 4.3.4% IFS=`:
bquote> `
zsh 4.3.4% set -- $(grep "$(date +%b%e)" fileX)
zsh 4.3.4% set -- $(egrep "$2|$3" fileY)
zsh 4.3.4% echo "You are oncall today $(date +%D)" "$2 $4"
You are oncall today 10/04/07  
zsh 4.3.4% IFS=':
quote> '
zsh 4.3.4% set -- $(grep "$(date +%b%e)" fileX)
zsh 4.3.4% set -- $(egrep "$2|$3" fileY)
zsh 4.3.4% echo "You are oncall today $(date +%D)" "$2 $4"
You are oncall today 10/04/07 jerry@foo.com grep@foo.com
zsh 4.3.4%
Reply With Quote
  #5  
Old 10-04-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
The date of the input file haven't the same format in your first and last post.
Try (not tested):
Code:
IFS=':
'
set -- $(grep "$(date +%m/%d/%y)" fileX)
set -- $(egrep "$2|$3" fileY)
mailx -s "You\'re oncall today $(date +%D)" "$2 $4"
Jean-Pierre.
Reply With Quote
  #6  
Old 10-04-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,855
Yes,
of course, Jean-Pierre is right,
the date format in the file sould match the grep command.
Reply With Quote
  #7  
Old 10-04-2007
Registered User
 

Join Date: Oct 2007
Posts: 11
Super Thanks!!!!

Both suggestions helped -

I change the tic from ` to ' and switched the date to the format Month day.

Looks Great

One last question -

My mailx command is not working - mailx works - but when I run the script or the mailx command alone, it seems like it's waiting for another input. For example, if I type: mailx -s "this site is great" foo@bar.com and hit enter, I get a new line return and it just sits there until I hit ctrl+D - at which point the line reads 'CC:' - hit Ctrl+D again and the message gets sent.

I looked through the manpages and a few posts on this site, but that seems like the general syntax for the mailx command.

Thanks
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0