0

I have a Python program that requires different parameters each time. The parameters are URLs stored in a file called urls.txt. So for example if urls.txt contains:

abc.com
xyz.com
def.com
mno.com

I'd want to execute:

python myProgram.py -u 'abc.com' -f output.txt
python myProgram.py -u 'xyz.com' -f output.txt
python myProgram.py -u 'def.com' -f output.txt
python myProgram.py -u 'mno.com' -f output.txt

How is this done?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1956609
  • 2,132
  • 5
  • 27
  • 43

1 Answers1

1
for url in `cat urls.txt`
do
    python myProgram.py -u "$url" -f output.txt
done

It can also be done with a while loop and read:

while read url
do
    python myProgram.py -u "$url" -f output.txt
done <urls.txt
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • 2
    I smell a useless use of cat ;-) http://stackoverflow.com/a/1521498/748858 – mgilson Jun 17 '14 at 04:52
  • 4
    The `cat` method has the advantage of not dropping the last line of the file if is is not terminated by newline. This can also be handled with the less readable version of while...read, `while read url || [[ -n $url ]]; do python myProgram.py -u "$url" -f output.txt; done` . Seems that the cat is not so useless :) – mhawke Jun 17 '14 at 05:04
  • Or even `xargs -n1 -Ifoo python myProgram.py -u "foo" -f output.txt < urls.txt`... – twalberg Jan 06 '15 at 19:35