How to use a pipe in a foreach statement
I have a for loop I'd like to run in bash like:
for i in user_* 开发者_运维问答do; cat $i | ./fetch_contact.php ; done;
Always gives an error like
-bash: syntax error near unexpected token `done'
I assume it has something to do with the pipe, but nothing I try to add in (parenthesis, etc) wrap the pipe sufficiently. How do you use a pipe in a command like this?
In Bash, do is a command. Also, it is for
not foreach
. Here's the fix:
for i in user_*; do cat $i | ./fetch_contact.php; done;
Turns out getting the semicolons and everything else right makes this whole pipe thing moot.
for i in user_*; do cat $i | ./fetch_contact.php; done;
Why loop?
cat user_* | ./fetch_contact.php
foreach i in user_*; do cat $i | ./fetch_contact.php ; done;
Semicolon should go before do
, not after.
No need for cat
:
for i in user_* ; do ./fetch_contact.php < "$i" ; done
here's one with some checking
for file in user_*
do
if [ -f "$file" ];then
./fetch_contact.php < "$file"
fi
done
精彩评论