开发者

How can I read standard input in a loop in bash, and then append the text to a file?

Say I have a bash script as follows

while
 read $f;
do
 cat $f >>开发者_JAVA百科 output.txt;
 echo "aaa" >> output.txt;
done

Yet the second echo statement isn't executed. At all. What am I doing wrong?

I'm running this via

tail -f /var/log/somelog | ./script.sh

$f should not be empty. It's only supposed to output when tail notices a change in the file.


The variable $f is probably empty, and your script is hanging on a call to cat with no arguments. Did you want to say

while read f

instead of

while read $f

?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜