Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a single-thread python script that gets used like this:

python -c "arg"

I also have a *.txt file with many arg, each in one single line in the respective *.txt. A text file, say 1.txt looks as follows:

arg1
arg2
arg3
arg4

Now I want to run the python script with all this arguments, one by one as the script does not allow more than one argument.

How can I process this arguments in batch mode using commandline?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

One way:

for i in *.txt
do
   while read line
   do
     python -c "$line"
   done < $i
done
share|improve this answer
    
Not working. The python script takes the text file as argument, not its content. –  mcbetz Jan 10 '13 at 13:33
    
@mmb : updated the solution –  Guru Jan 10 '13 at 13:57
    
Totally does the job now! –  mcbetz Jan 10 '13 at 14:54

A very easy solution,

cat 1.txt | xargs -I{} python -c "{}"

Explanation: cat prints all lines and xargs makes sure each line is used as argument.

Edit: the -I{} means that {} should by replaced by the argument.

share|improve this answer
    
Wow, that is indeed easy. But does it take into consideration, that the arguments have to be enclosed by ""? –  mcbetz Jan 10 '13 at 14:56
    
Now it does, see my edit ;) If you have multiple files, simply replace 1.txt by *.txt and it should work as well. –  Gerhard Burger Jan 10 '13 at 15:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.