2011-04-11 324 views
2

我一直在編寫一個unix腳本,它將掃描文本文件,查找特定單詞的實例,然後刪除另一個文檔中的相應行。一切都在努力,直到我去測試我最近的嘗試,我得到:意外標記'done'附近的語法錯誤

./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 

回答

0

我看了for/done例子,他們表現出它作爲:

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

也就是說,只有一個done末。你有兩個。

+0

嗨taspeotis,謝謝你看我的問題!拿走其中一個人仍然給我同樣的錯誤。 ./Present.sh:第35行:語法錯誤附近的意外令牌'完成' ./Present.sh:第35行:'完成' – Dave 2011-04-11 03:00:56

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) 
+0

嗨Random832,感謝您的建議。不幸的是,它沒有奏效。 :-(我被給出了以下錯誤:./Present.sh:第35行:語法錯誤附近的意外令牌'完成' ./Present.sh:第35行:'\t完成'' – Dave 2011-04-11 02:59:06

+0

嘗試使用'elif'更改。事實上,'()'可能沒有必要 – Random832 2011-04-11 03:01:04

+0

它的工作! Random832,你是最棒的 - 非常感謝你! – Dave 2011-04-11 03:02:19

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 
相關問題