繁体   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