【问题标题】:Syntax error near unexpected token 'done'意外标记“完成”附近的语法错误
【发布时间】:2011-08-02 17:53:39
【问题描述】:

我一直在编写一个 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

【问题讨论】:

    标签: shell unix loops syntax while-loop


    【解决方案1】:

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

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

    即最后只有一个done。你有两个。

    【讨论】:

    • 您好 taspeotis,感谢您查看我的问题!拿走其中一项仍然给我同样的错误。 ./Present.sh:第 35 行:意外标记附近的语法错误 done' ./Present.sh: line 35: done '
    【解决方案2】:

    尝试将 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 )
    

    【讨论】:

    • 嗨 Random832,感谢您的建议。不幸的是,它没有用。 :-( 我收到以下错误:./Present.sh: line 35: syntax error near unexpected token done' ./Present.sh: line 35: done )'
    • 尝试更改elif。事实上,( ) 可能没有必要
    【解决方案3】:

    就目前而言,您的代码相当于:

    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
    

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 2015-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      相关资源
      最近更新 更多