“如何修复”;意外标记“完成”附近出现语法错误&引用;在bash中的嵌套循环中?

“如何修复”;意外标记“完成”附近出现语法错误&引用;在bash中的嵌套循环中?,bash,Bash,我正在编写一个脚本,它将在列中循环查找一个单词的实例 我决定通过嵌套循环执行,在执行代码后,会出现以下错误: ./gallupscript.sh:第115行:意外标记附近的语法错误done' ./gallupscript.sh:第115行:done' 以下是我的代码失败的区域: token=2 #token is the column number starter=0 s1="First" ; s2="Second" ; s3="Third" ; s4="Fourth" ; s5="Fifth"

我正在编写一个脚本,它将在列中循环查找一个单词的实例

我决定通过嵌套循环执行,在执行代码后,会出现以下错误:

./gallupscript.sh:第115行:意外标记附近的语法错误
done'
./gallupscript.sh:第115行:
done'

以下是我的代码失败的区域:

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

此代码将输出与我正在搜索的单词匹配的名称(在第一列中)。

您没有关闭
语句,如果
语句与
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
token=2#token是列号
起动器=0
s1=“第一”;s2=“第二”;s3=“第三”;s4=“第四”;s5=“第五”
s=s;a=1
而[$token-le 6]
做
cat gallup.csv | cut-d','-f“$token”| grep-n$strength1 | cut-d':'-f1>str1
如果[-s str1]
然后
对于i,单位为美元(类别str1)
做
如果[$i-ne$number&$starter-eq 0]]
然后
save=$(cat gallup.csv | head-$i | tail+$i | cut-d','-f1)
s=s;s+=$a;起动器=1
printf“-$strength1--”
printf“${!s}强度:$save”
elif[$i-ne$number&$starter-ne 0]]
然后
save=$(cat gallup.csv | head-$i | tail+$i | cut-d','-f1)
printf“,$save”
fi
完成

fi#将准确地告诉您错误所在:您的
if[-s str1]
没有
fi
。顺便说一句,请参阅;使用
printf
时,使用带有占位符的格式字符串作为变量(
printf'-%s-->“$strength1”
,或
printf',%s'$save”
),以防止格式说明符误解这些变量的内容;并修复其他各种确定的报价问题。噢,哇,非常感谢!我不敢相信我错过了它哈哈…我真的不知道为什么你要使用
str1
文件,而不仅仅是将管道捕获到一个变量中。甚至可以将其直接捕获到一个数组:
readarray-t items<感谢这一技巧,我一定会使用它。在“回答好的问题”一节中,请注意“在帮助中心定义”链接,以及该链接后面的“一些问题仍然与主题无关”列表中的#2。由拼写错误引起的问题应该关闭,而不是回答——通过添加答案,您参与阻止OP删除问题本身(另一个参与者是任何投票人)。