read from a file to a list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read from a file to a list
# 1  
Old 11-18-2008
read from a file to a list

Hello there,

I have a file that contents a list of email address, and each is separated by new lines. I want to read the first email address into TO: filed and the rest into a list and goes to cc field. I plan to use mailx to send the email. I inserted the email addresses into an array, but what can I do to turn the array into a list, so I can pass it to mailx command? The script is working, but it prints list of cc one by one. That won't work for mailx since I need all of them in a list. Thanks!

#read email address line by line, the first line always goes to TO field
fname="/home/.../emailFile.txt"
cc[100]="" #list of cc receipients

exec<$fname
value=0

while read line
do
value=`expr $value + 1`;
if [ $value -eq 1 ]
then
headit=$line
echo "headit email is: $headit" #receipient email address, always the first line in the file
else
cc[$value]=$line
echo "it contacts email: ${cc[$value]}"
fi
done

#mailx command to send email with a text body, attachment, cc, bcc, and replying address,
echo "test" | cat - body.txt | mailx -a attachment.html -b "bcc@mail.com" -c "$cc" -s "test" -r "reply@mail.com" "$headit"
# 2  
Old 11-18-2008
Use [ code ] and [ /code ] tags when posting logs, code etc., ty.

Code:
root@isau02:/data/tmp/testfeld> cat infile
a@some.net
b@some.net
c@some.net
d@some.net
root@isau02:/data/tmp/testfeld> V_TO=`head -1 infile`
root@isau02:/data/tmp/testfeld> echo $V_TO
a@some.net
root@isau02:/data/tmp/testfeld> V_CC=`awk 'NR > 1 {print}' infile`
root@isau02:/data/tmp/testfeld> echo $V_CC
b@some.net c@some.net d@some.net

When using the variables, maybe use "${VAR}" to make sure you get no troubles.
# 3  
Old 11-18-2008
Hi,

to read all emails into an array:

Code:
email=( $(cat file) )

Code:
TO=$email[1]

to convert the array to a list:

Code:
CC=$(for i in {2..${#email[@]}}; do printf "%s " ${email[$i]}; done)

Which iterates over the array starting at position 2 for the length of the array, the it prints the current array followed by a space but no line break. The result is passed to the variable CC. Now you have the TO in $TO and the CC in $CC.

HTH

Chris
# 4  
Old 11-18-2008
Hi,

there is a small error in there. It has to be:

cc=$(for i in {2..${#email[@]}}; do printf "%s, " ${email[$i]}; doneSmilie

The comma was missing as you need a comma separated list of repicipients.

I don't use mailx but mutt. The following line does the trick for me:

echo test | mutt -s test -c "$cc" -- $to

HTH Chris
# 5  
Old 11-18-2008
Thanks for everyone's posts. I really appreciate it. Chris's code has not worked for me though, this is the output:

someone@mail.com[1]
{2..2}: syntax error: operand expected (error token is "{2..2}") #maybe because I only have two email address in the file. It is possible that the file only contents 1 or 2 email addresses.

Zaxxon's code worked for me. Thanks again for all the inputs.

Last edited by pinkgladiator; 11-18-2008 at 01:35 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a lis, find items in a file from the list, change each item

Hello, I have some tab delimited text data, file: final_temp1 aname val NAME;r'(1,) 3.28584 r'(2,)<tab> NAME;r'(3,) 6.13003 NAME;r'(4,) 4.18037 r'(5,)<tab> You can see that the data is incomplete in some cases. There is a trailing tab after the first column for each incomplete row. I... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

2. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

3. UNIX for Dummies Questions & Answers

Read workstation list from file und run command

Hi, how do I read in a file which includes a list of workstations and then run a command for each workstation ? I am unclear which command to use to read in , or is this not possible ? Thanks, (3 Replies)
Discussion started by: manni2
3 Replies

4. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

5. Shell Programming and Scripting

How to read a list of paths from a file?

Hi everyone! I'm pretty bad at shell scripting and I am trying to create a java launcher. The idea is to store in a configuration file different paths (each for a different java version) and then, run a .sh file that would read the specified java path and execute the .jar file. This is what i have... (7 Replies)
Discussion started by: mostacholoco
7 Replies

6. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

7. Shell Programming and Scripting

Need help with awk - how to read a content of a file from every file from file list

Hi Experts. I need to list the file and the filename comes from the file ListOfFile.txt. Basicly I have a filename "ListOfFile.txt" and it contain Example of ListOfFile.txt /home/Dave/Program/Tran1.P /home/Dave/Program/Tran2.P /home/Dave/Program/Tran3.P /home/Dave/Program/Tran4.P... (7 Replies)
Discussion started by: tanit
7 Replies

8. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies

9. Shell Programming and Scripting

read list of filenames from text file, archive, and remove

I posted a week ago regarding this scripting question, but I need to revisit and have a few more questions answered.. User cfajohnson was extremely helpful with the archive script, but clarification on my part is needed to help steer the answer in a direction that works in this particular... (5 Replies)
Discussion started by: fxvisions
5 Replies

10. Shell Programming and Scripting

read a list one at a time

just have a muddled head at the moment... bare with me. say i have a variable $count... and a list in a file i want to use the $count line from that textfile but don't seem to recall how to access it? eg user=`cat file1.txt` # user to be $count line in that list? eg david... (7 Replies)
Discussion started by: nortypig
7 Replies
Login or Register to Ask a Question