Ksh script - Design ? - Search file and set variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ksh script - Design ? - Search file and set variables
# 1  
Old 10-03-2007
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
# 2  
Old 10-03-2007
Code:
IFS=':
'
set -- $(grep "$(date +%b%e)" fileX)
set -- $(egrep "$2|$3" fileY)
mailx -s "You\'re oncall today $(date +%D)" "$2 $4"

# 3  
Old 10-04-2007
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
# 4  
Old 10-04-2007
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%

# 5  
Old 10-04-2007
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.
# 6  
Old 10-04-2007
Yes,
of course, Jean-Pierre is right,
the date format in the file sould match the grep command.
# 7  
Old 10-04-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

<< Environmental Variables are not set when script completes >>

Hi Team, I have a wrapper script which i have pasted below, it internally calls one python script to generate Environmental in a file called /home/oracle/myenv.sh, when i execute this script via wrapper script, its not reflecting in my current session, still showing old env variables. any... (2 Replies)
Discussion started by: kamauv234
2 Replies

2. Programming

[LUA] Set variables according to if file exists

Heya I'm using Awesome WM with the Vicious widget library. As i'm using multi boot, Win8, Fedora and Arch, i have my WM-Config shared accross my GNU/Linux installations. The regarding snippet: -- Functions -- Its just a workaround for an 'unstable' 'hwmon/hwmon' definition of Fedora21 -... (0 Replies)
Discussion started by: sea
0 Replies

3. Shell Programming and Scripting

Assigning multiple variables in ksh script

Is it possible to have a user input multiple words in one line and have the script assign each word a variable? I'm stuck please assist. Example using "BILL JOHN SARA JILL" as what the user could type: printf "Enter account names: " BILL JOHN SARA JILL read input (9 Replies)
Discussion started by: seekryts15
9 Replies

4. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

5. Shell Programming and Scripting

set ksh script in restricted mode

Hi, I came across a post wherein you can use "set -r"(on bash) to activate restricted mode ( wherein you cant run some commands such as cd etc). Can anyone guide if we have anything similar in ksh ? Thanks (2 Replies)
Discussion started by: Shivdatta
2 Replies

6. Shell Programming and Scripting

Set variables from file, then loop

Hello, I am very, very new to shell scripting, but what I'm attempting to do is read in a list of user ID's to create on a database system from a CSV flat file, and for each entry run the "create user" script. I've gotten pretty far but I'm having trouble with the looping mechanism.... Any... (8 Replies)
Discussion started by: jkarren
8 Replies

7. Shell Programming and Scripting

Unable to set variables in Linux script

Hi, I am pretty new in writing shell script on LINUX, I tried to write the script just like KSH on HP unix but it seems nothing is working. Even assigning variables seems to be not working as KSH on HP unix. Please help in resolving the issues I am facing on LINUX. I have declared variables for... (5 Replies)
Discussion started by: smr.ryl
5 Replies

8. Shell Programming and Scripting

KSH script eval(?) to set variable

first of all, thanks to all on this board, it has been a huge resource to answer most of my questions! I am stuck on something that should really be simple, and was looking for some help.. I am using KSH on solaris and working on a script to move containers from server to server. Where i am... (4 Replies)
Discussion started by: tksol
4 Replies

9. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies
Login or Register to Ask a Question