LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2017, 09:02 AM   #1
jhdore
LQ Newbie
 
Registered: Sep 2017
Posts: 2

Rep: Reputation: Disabled
Passing a list to a script


Hi all,

I've got about 70 Mailman mailing lists I need to extract the members for into individual text files. I have the list names in a single file, but can't figure out how to use each line in that list as the listname for Mailman's list_members command.

I envisaged something like

Code:
for i in listnames.txt

do 
/usr/lib/mailman/bin/list_members $i > $i.txt
done
but it fails with error 'No such list: listnames.txt'

This is my first foray into shell scripting, so I'm thrashing around in the dark somewhat - can anyone enlighten me?

Many thanks,
J
 
Old 09-29-2017, 09:07 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,487

Rep: Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907
Try:

Code:
 for i in $(cat listnames.txt)
 
1 members found this post helpful.
Old 09-29-2017, 09:08 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,567

Rep: Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923
Code:
while read i
do
    /usr/lib/mailman/bin/list_members $i > $i.txt
done < listnames.txt
More info:

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?

Add "-r" to read if you need it (or if you want).

Last edited by dugan; 09-29-2017 at 09:15 AM.
 
3 members found this post helpful.
Old 09-29-2017, 09:09 AM   #4
jhdore
LQ Newbie
 
Registered: Sep 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
Try:

Code:
 for i in $(cat listnames.txt)
Excellent - job done!

Thanks loads.
J
 
Old 10-11-2017, 06:04 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
Quote:
Originally Posted by jhdore View Post
Excellent - job done!

Thanks loads.
J
The solution posted by dugan is the recommended way to operate on file contents. Read the link he provided, and also this: Why you don't read lines with "for".

But to summarize, a for loop is designed to iterate over a list of words. What's happening above is that $(cat filename) is simply dumping the entire contents of the file into the loop at once, and since the $() substitution is unquoted the shell proceeds to chop it up into individual words to be processed. Depending on the contents and size of the file, this could have unexpected and deleterious consequences.

A while read loop, on the other hand, draws only one line (configurable) from the input file into the variable (or variables) for each iteration of the loop, until the file is exhausted. It is quite a bit more robust and gives you more power to control what gets read, and when, and how.

If you're truly desperate to use a for loop, however, it's also possible to use bash's mapfile built-in to read each line into an array first, then iterate over that.

Code:
mapfile -t list <inputfile.txt

for line in "${list[@]}"; do
   echo "$line"
done
mapfile puts each line of text into a separate array entry (the -t option removing the newlines at the end), and "${list[@]}", when quoted, expands them inside the loop again so that each line is treated as a separate "word".
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem passing variable in a list smturner1 Linux - Newbie 6 10-16-2012 10:07 AM
[SOLVED] Passing List of files to 7zip exe laks Programming 6 04-11-2012 03:30 PM
[SOLVED] Passing a list of variables to a for loop pdr_dan Linux - Newbie 5 03-21-2011 12:38 PM
C++ passing variable argument list to other functions R00ts Programming 8 08-08-2008 05:10 PM
passing a list of arguments to a command hdagelic Linux - General 2 05-09-2005 09:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration