意外令牌'完成'附近的语法错误

时间:2011-04-11 02:48:13

标签: shell unix loops syntax while-loop

我一直在编写一个unix脚本,它将扫描文本文件,查找特定单词的实例,然后删除另一个文档中的相应行。一切都在努力,直到我去测试我最近的尝试,我得到了:

./Present.sh: line 35: syntax error near unexpected token `done'
./Present.sh: line 35: `    done'

我不明白为什么。谷歌搜索错误给了我一些unix.com的回复,但没有任何价值(尝试其他有这些问题的人的建议没有帮助解决问题)。非常感谢你的帮助!

我的代码是:

for var in bent bound bled bred brought built burned burnt bought caught clung crept dealt dug dived dreamed dreamt fed felt fought found fled flung ground hung heard held kept knelt laid led leaped leapt learned learnt left lent lighted lost made meant met misspelled misspelt mowed mown paid pled proved proven sawed sawn said sought sold sent sewed sewn shaved shaven shone shoed shod shot showed sat slept slid slung sowed sown sped spent silted spilt spun sprung stood stuck stung struck strung swept swelled swollen swung taught told thought thrived understood upheld waved woven wept wound won withheld withstood wrung 
do
    cd ~
    cd Documents/UPenn/'Senior Year'/'Spring 2011'/Thesis/Results/
    echo "$var"
    NUMLINE=0
    cat 'foradam.txt' | while IFS=$'\t' read -r word pos other; do
        if [ "$word" = '#' ] || [ "$word" = Number ] || [ "$word" = CHI ]; then
            NUMLINE=`expr $NUMLINE + 1`
            echo "error 1"
            echo "$word"
            echo "$NUMLINE"
        if [ "$word" = $var ] && [ "$pos" = VVN ];then 
            sed -i '$NUMLINE' d Brown_Adam_CIVForms.txt
            echo "error 4"
                echo "$word"
            echo "$NUMLINE"
        else
            echo "nothing on this line"
            echo "$word"
            echo "$NUMLINE"
        fi
    done
done

3 个答案:

答案 0 :(得分:2)

目前,您的代码相当于:

for var in bent bound bled bred brought built burned burnt bought caught clung \
          crept dealt dug dived dreamed dreamt fed felt fought found fled flung \
          ground hung heard held kept knelt laid led leaped leapt learned learnt \
          left lent lighted lost made meant met misspelled misspelt mowed mown \
          paid pled proved proven sawed sawn said sought sold sent sewed sewn \
          shaved shaven shone shoed shod shot showed sat slept slid slung sowed \
          sown sped spent silted spilt spun sprung stood stuck stung struck \
          strung swept swelled swollen swung taught told thought thrived \
          understood upheld waved woven wept wound won withheld withstood wrung 
do
    cd ~
    cd Documents/UPenn/'Senior Year'/'Spring 2011'/Thesis/Results/
    echo "$var"
    NUMLINE=0
    cat 'foradam.txt' | while IFS=$'\t' read -r word pos other; do
        if [ "$word" = '#' ] || [ "$word" = Number ] || [ "$word" = CHI ]; then
            NUMLINE=`expr $NUMLINE + 1`
            echo "error 1"
            echo "$word"
            echo "$NUMLINE"
            if [ "$word" = $var ] && [ "$pos" = VVN ];then 
                sed -i '$NUMLINE' d Brown_Adam_CIVForms.txt
                echo "error 4"
                echo "$word"
                echo "$NUMLINE"
            else
                echo "nothing on this line"
                echo "$word"
                echo "$NUMLINE"
            fi
        fi # This fi is missing in your code!!!
    done
done

缺少“fi”,这使“done”意外发生。另一种诊断是嵌套的“if”实际上应该是“elif”:

for var in bent bound bled bred brought built burned burnt bought caught clung \
          crept dealt dug dived dreamed dreamt fed felt fought found fled flung \
          ground hung heard held kept knelt laid led leaped leapt learned learnt \
          left lent lighted lost made meant met misspelled misspelt mowed mown \
          paid pled proved proven sawed sawn said sought sold sent sewed sewn \
          shaved shaven shone shoed shod shot showed sat slept slid slung sowed \
          sown sped spent silted spilt spun sprung stood stuck stung struck \
          strung swept swelled swollen swung taught told thought thrived \
          understood upheld waved woven wept wound won withheld withstood wrung 
do
    cd ~
    cd Documents/UPenn/'Senior Year'/'Spring 2011'/Thesis/Results/
    echo "$var"
    NUMLINE=0
    cat 'foradam.txt' | while IFS=$'\t' read -r word pos other; do
        if [ "$word" = '#' ] || [ "$word" = Number ] || [ "$word" = CHI ]; then
            NUMLINE=`expr $NUMLINE + 1`
            echo "error 1"
            echo "$word"
            echo "$NUMLINE"
        elif [ "$word" = $var ] && [ "$pos" = VVN ]; then 
            sed -i '$NUMLINE' d Brown_Adam_CIVForms.txt
            echo "error 4"
            echo "$word"
            echo "$NUMLINE"
        else
            echo "nothing on this line"
            echo "$word"
            echo "$NUMLINE"
        fi
    done
done

答案 1 :(得分:1)

尝试将while循环放在子shell中。还有,一个 您的if语句缺少elif

cat 'foradam.txt' | ( while IFS=$'\t' read -r word pos other; do
    if [ "$word" = '#' ] || [ "$word" = Number ] || [ "$word" = CHI ]; then
        ...
    elif [ "$word" = $var ] && [ "$pos" = VVN ]; then 
        ...
    else
        ...
    fi
done )

答案 2 :(得分:-1)

我查看了for / done示例,他们将其显示为:

#!/bin/bash
for i in $( ls ); do
    echo item: $i
done

也就是说,最后只有一个done。你有两个。