Extract arguments from stdout and pipe
I was trying to execute a script n times with a different fi开发者_如何学Pythonle as argument each time using this:
ls /user/local/*.log | xargs script.pl
(script.pl accepts file name as argument)
But the script is executed only once. How to resolve this ? Am I not doing it correctly ?
ls /user/local/*.log | xargs -rn1 script.pl
I guess your script only expects one parameter, you need to tell xargs about that.
Passing -r helps if the input list would be empty
Note that something like the following is, in general, better:
find /user/local/ -maxdepth 1 -type f -name '*.log' -print0 |
xargs -0rn1 script.pl
It will handle quoting and directories safer.
To see what xargs actually executes use the -t
flag.
I hope this helps:
for i in $(ls /usr/local/*.log)
do
script.pl $i
done
I would avoid using ls
as this is often an alias on many systems. E.g. if your ls
produces colored output, then this will not work:
for i in ls
; do ls $i;done
Rather, you can just let the shell expand the wildcard for you: for i in /usr/local/*.log; do script.pl $i; done
精彩评论