Operating System - HP-UX
1832780 Members
3027 Online
110045 Solutions
New Discussion

Script problems attributing variables

 
SOLVED
Go to solution
Guilherme Belinelo
Occasional Advisor

Script problems attributing variables

Hi,

I?m making a script to send several emails and the addresses are defined as variables. The problem is that I?m trying to use a single line for the email part but it?s not working. This part of the script is like this:

export NUM=3 #number of email addresses
export EMAIL1=test1@test.com
export EMAIL2=test2@test.com
export EMAIL3=test3@test.com


while [ $NUM -ne 0 ]
do
EMAIL="EMAIL$NUM"
echo test | mailx -s"test subject" $EMAIL
let "NUM = NUM - 1"
done

The $EMAIL shows EMAIL1, EMAIL2 and EMAIL3 but I wanted it to assume the real email addresses.

I asked a friend who works with java script and he told me to use arrays. Do we have that for shell scripting ??

Does anybody now how to do it in any way ??
4 REPLIES 4
Mark Fenton
Esteemed Contributor

Re: Script problems attributing variables

getting too fancy for me.

Would an alias work as well, or do you need to have some sort of conditional logic to determine which addresses to send to?

If the list is static, then there'd be no need to invent fancy variables.

Alternatively, would it work as well to employ a case statement in place of your do/done?

Just a thought.
Guilherme Belinelo
Occasional Advisor

Re: Script problems attributing variables

Mark,

The email list is static and I don?t need to use any logic to determine the addresses. Actually it shold be very simple but I don?t know how to do it.

Can you send me some example using alias ? It could help a lot once I don?t know how to use it.

Tks,

Guilherme Belinelo

John Poff
Honored Contributor
Solution

Re: Script problems attributing variables

Guilherme,

I took your script and modified it to use an array. Here is what it looks like:

#!/bin/sh

export NUM=3 #number of email addresses
export EMAIL[1]=test1@test.com
export EMAIL[2]=test2@test.com
export EMAIL[3]=test3@test.com


while [ $NUM -ne 0 ]
do
EMAIL="EMAIL$NUM"
#echo test | mailx -s"test subject" $EMAIL
echo "EMAIL[$NUM] = ${EMAIL[$NUM]}"
let "NUM = NUM - 1"
done

I remarked out the line that does the 'mailx', just for testing purposes, and I put in a line to echo the value of the EMAIL array variable. When I run it, here is the output:

EMAIL[3] = test3@test.com
EMAIL[2] = test2@test.com
EMAIL[1] = test1@test.com


Does this help? Also, for the 'mailx' command you can specify the e-mail addresses separated by commas. This means you can create one variable with all the addresses in it and use it something like this:

EMAIL="test1@test.com, test2@test.com, test3@test.com"

echo test | mailx -s"Test subject" $EMAIL

I hope that helps.

Good luck!

Rodney Hills
Honored Contributor

Re: Script problems attributing variables

If you had the list of email addresses in a file, you could

cat emaillist | xargs -n1 mailx -s "mysubject"

This would run mailx for each line in emaillist, appending the line on the end of the mailx command.
There be dragons...