[英]I get “syntax error near unexpected token `done' ” when I use while loop in bash
I have a file which filled in streaming way line by line. 我有一个文件,其中逐行填写了流式传输方式。 I need to decrease the volume of the file by deleting the oldest record in it. 我需要通过删除文件中最旧的记录来减少文件的容量。 I would like to count the number of lines and if the number of lines exceed than 100 then delete the oldest lines. 我想计算行数,如果行数超过100,则删除最旧的行。 However I got the following error: 但是我收到以下错误:
./1.sh: line 18: syntax error near unexpected token `done'
./1.sh: line 18: `done'
Here is my code: 这是我的代码:
#!/bin/bash
FILE="11.txt"
linenum=0
while true; do
#Count number of lines
linenum=`cat "$FILE" | wc -l`
while [ $linenum -gt 100 ] do
#Delete the head of file (oldest)
sed -i 1,1d "$FILE"
#Count number of lines
linenum=`cat "$FILE" | wc -l`
done
done
Can you please help me? 你能帮我么?
You need a linefeed or a ;
您需要换行或;
between the while
's condition and the do
: 在while
的条件和do
:
while [ $linenum -gt 100 ]; do
#Delete the head of file (oldest)
sed -i 1,1d "$FILE"
#Count number of lines
linenum=$(wc -l "$FILE")
done
I also indented the code properly, changed the subshell `...`
notation to the more modern $(...)
and removed a redundant use of cat
. 我还适当地缩进了代码,将subshell的`...`
表示法更改为更现代的$(...)
并删除了cat
的多余用法。
You missed the semicolon at below line 您错过了下面一行的分号
while [ $linenum -gt 100 ] do
should be 应该
while [ $linenum -gt 100 ] ; do
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.