问题
I am working on a program that does the following:
Reads 270 file in the folder X and calls another program to run a command with the contents of the file. For example:
$ cat X/1.txt
AAAA
BBBB
CCCC
The program is supposed to read that text (there are 270 of them) and in the loop it's supposed to do:
$python file.py AAAA;
then
python file.py BBBB;
then
python file.py CCCC.
and the move on the next file like 22.txt (the files aren't numeric so a counter wouldn't be much help).
I tried the following:
function text(){
for i in $(ls Domains/);
while read line;
do
$line
f= done < Domains/$i.txt
python file.py $f
}
text;
Thanks, guys!
回答1:
This is where xargs comes in handy: Concatenate all the files and pipe that into xargs which feeds each line to the python program one by one.
cat * | xargs -L1 python file.py
回答2:
This should work:
function text(){
for i in $(ls Domains/)
do
while read line;
do
python file.py $line
done < Domains/$i.txt
done
}
来源:https://stackoverflow.com/questions/29836900/running-a-command-for-each-line-of-a-text-file-bash