簡體   English   中英

如何在bash的嵌套循環中修復“意外令牌'done'附近的語法錯誤”?

[英]How to fix “syntax error near unexpected token `done' ” in a nested loop in bash?

我正在編寫一個腳本,該腳本將遍歷各列以查找單詞的實例。

我決定通過嵌套循環來執行此操作,並在執行代碼后收到此錯誤:

./gallupscript.sh:行115:附近意外的標記語法錯誤done' ./gallupscript.sh: line 115:完成”

這是我的代碼失敗的區域:

token=2 #token is the column number
starter=0
s1="First" ; s2="Second" ; s3="Third" ; s4="Fourth" ; s5="Fifth"
s=s ; a=1
while [ $token -le 6 ]
do
    cat gallup.csv | cut -d',' -f"$token" | grep -n $strength1 | cut -d':' -f1 > str1
    if [ -s str1 ]
    then
        for i in $(cat str1)
        do
            if [[ $i -ne $number && $starter -eq 0 ]]
            then
                save=$(cat gallup.csv | head -$i | tail +$i | cut -d',' -f1)
                s=s ; s+=$a ; starter=1
                printf "-- $strength1 --"
                printf "${!s} Strength: $save"
            elif [[ $i -ne $number && $starter -ne 0 ]]
            then
                save=$(cat gallup.csv | head -$i | tail +$i | cut -d',' -f1)
                printf ", $save"
            fi
        done
    starter=0
    a=$((a+1))
    token=$((token+1))
    echo #new line
done

預期此代碼將輸出名稱(在第一列中),其中該單詞與我要搜索的單詞匹配。

您不會關閉if語句,它與for無關。

請使用以下代碼:

token=2 #token is the column number
starter=0
s1="First" ; s2="Second" ; s3="Third" ; s4="Fourth" ; s5="Fifth"
s=s ; a=1
while [ $token -le 6 ]
do
    cat gallup.csv | cut -d',' -f"$token" | grep -n $strength1 | cut -d':' -f1 > str1
    if [ -s str1 ]
    then
        for i in $(cat str1)
        do
            if [[ $i -ne $number && $starter -eq 0 ]]
            then
                save=$(cat gallup.csv | head -$i | tail +$i | cut -d',' -f1)
                s=s ; s+=$a ; starter=1
                printf "-- $strength1 --"
                printf "${!s} Strength: $save"
            elif [[ $i -ne $number && $starter -ne 0 ]]
            then
                save=$(cat gallup.csv | head -$i | tail +$i | cut -d',' -f1)
                printf ", $save"
            fi
        done
    fi   #    <------------ add this line
    starter=0
    a=$((a+1))
    token=$((token+1))
    echo #new line
done

暫無
暫無

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

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