1829770 Members
3129 Online
109992 Solutions
New Discussion

Script help needed

 
TheJuiceman
Super Advisor

Script help needed

OK...let me see if I can even explain what I'm trying to do here...

I have a file (file1) that gets generated with a list in it like so..

ABC
DEF
GHI

I have another file (file2) with variables in it like so...

BOB="bob@mail.com"
ABC="$BOB"

I am wanting to write a script to do something like this...

. /file2
while read ENTRY
do
grep "$ENTRY" /tmp/filegenfromanotherscript > /tmp/new
FILE=/tmp/new
LIST=$(cat /tmp/new)
if [[ -s ${FILE} ]]; then
cat < /tmp/mail.msg
Here's my message
$LIST
EOM
(echo "Subject: This is it"
cat /tmp/mail.msg
) | rmail RIGHT HERE IS THE PROBLEM
fi
done < /tmp/file1

What I am wanting to do is this: The ENTRY variable will be the same as list in file1 (ie. ABC, DEF, etc). I want to be able to have the script use the ENTRY variable, find that variable in file2 and then send the email to the appropriate address.

File1 is generated regularly and cannot change. Filegenfromanotherscript is fixed (this is just a file with a listing of things I am grepping information from. This file is generated regularly too)

How can I write this script to accomplish what I am trying to do? Thanks for the help.
3 REPLIES 3
Matti_Kurkela
Honored Contributor

Re: Script help needed

Hmm... you need to expand variables named by other variables, so you're going to need "eval" for that. You may also need the variables of file2 exported, so that sub-shells can read them.

Let me try to re-write your script...

#!/bin/sh

set -a # auto-export all variables after this
. /file2
set +a # stop auto-export

while read ENTRY
do
ADDRESS=$(eval echo \$$ENTRY)
LIST=$(grep "$ENTRY" /tmp/filegenfromanotherscript)
if [ "$LIST" != "" ]; then
cat << EOM | mail -s "This is it" $ADDRESS
Here's my message
$LIST
EOM
fi
done < /tmp/file1

... hmm, I think I got rid of most of those temporary files :-)

MK
MK
TheJuiceman
Super Advisor

Re: Script help needed

I appreciate the help. However, I cannot seem to get your script to work. Any thoughts? Thanks.
TheJuiceman
Super Advisor

Re: Script help needed

Actually, with a little changing I got it to do what I want. Thank you very much!!! Please repost a reply so I can give you more points. Thanks!!!