我遇到了循环问题,我在脚本中使用了 while ,如下所示:
while read LINE
do {
}
done
但问题是每次运行脚本时我都应该按回车键,而且它应该执行 50000 次!我怎样才能将其更改为自动运行的循环?
我可以使用以下命令计算文件的长度:lenght =`wc -l <myfile然后使用它吗?或者有更好的解决方案?
答案1
您可以使用 for 循环吗?
for i in {1..5}
do
echo "I have $i bottle(s) of Mountain Dew"
done
I have 1 bottle(s) of Mountain Dew
I have 2 bottle(s) of Mountain Dew
I have 3 bottle(s) of Mountain Dew
I have 4 bottle(s) of Mountain Dew
I have 5 bottle(s) of Mountain Dew
您甚至可以向后循环!
for i in {5..1}
do
echo "I have $i bottle(s) of Mountain Dew"
done
I have 5 bottle(s) of Mountain Dew
I have 4 bottle(s) of Mountain Dew
I have 3 bottle(s) of Mountain Dew
I have 2 bottle(s) of Mountain Dew
I have 1 bottle(s) of Mountain Dew
答案2
你的脚本 < 5000_lines.dat
在您的while中您应该检查“非EoF”并将其放入read
循环中。
答案3
尝试这个:
yes "" | while ....
这会发送你的 while-body 可以使用的换行符流。