Syntax error near unexpected token 'done'

21,407

Solution 1

As it stands, your code is equivalent to:

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

There is a 'fi' missing, which makes the 'done' unexpected. An alternative diagnosis is that the nested 'if' should actually be an '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

Solution 2

Try putting the while loop in a subshell. Also, one of your if statements is missing an 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 )
Share:
21,407
Dave
Author by

Dave

Updated on December 31, 2020

Comments

  • Dave
    Dave over 3 years

    I have been writing a unix script that will scan through a text file, find instances of a specific word, and then delete a corresponding line in another document. Everything was working up until I went to test my most recent attempt, and I am getting:

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

    and I have no idea why. Googling the error gives me a few responses from unix.com, but nothing of value (trying the suggestions of others who had these problems did not help the problem). Thanks so much for the help!!!

    My code is:

    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