簡體   English   中英

當我在bash中使用while循環時,出現“意外令牌'done'附近的語法錯誤”

[英]I get “syntax error near unexpected token `done' ” when I use while loop in bash

我有一個文件,其中逐行填寫了流式傳輸方式。 我需要通過刪除文件中最舊的記錄來減少文件的容量。 我想計算行數,如果行數超過100,則刪除最舊的行。 但是我收到以下錯誤:

./1.sh: line 18: syntax error near unexpected token `done'
./1.sh: line 18: `done'

這是我的代碼:

#!/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

你能幫我么?

您需要換行或; 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

我還適當地縮進了代碼,將subshel​​l的`...`表示法更改為更現代的$(...)並刪除了cat的多余用法。

您錯過了下面一行的分號

while [ $linenum -gt 100 ] do

應該

while [ $linenum -gt 100 ] ; do

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM