2016-11-24 65 views
1

我寫了一個bash腳本,並不斷收到「意外的EOF」錯誤。我找不到它的問題。任何人都可以請我指出正確的方向。任何提示如何我可以做得更好,也將不勝感激。爲什麼我在這個bash腳本中一直收到「Unexpected EOF」?

Picture of the Error I get

這裏是實際的腳本

figlet -ctf slant "3-Way-Riddles" 
toilet -f term -F border --gay "Remember, all the questions have a \"ONE WORD\" answer!!!" 

read -p "Please press the [Enter] key to continue: " 

read -p "Player One, please enter your name: " playerOne 
read -p "Player Two, please enter your name: " playerTwo 

one="0" 
two="0" 

function playerName() { 
    read -p "Who is answering these questions? " name 
     if [[ "${name,,}" != "${playerOne,,}" && "${name,,}" != "${playerTwo,,}" ]] ; then 
      echo -e "Wrong name input, please enter your name again.\n" 
      playerName 
    fi 
} 

function points() { 
    if [ "$name" = "$playerOne" ] ; then 
      one=$((one+1)) 
      return $one 
    else 
     two=$((two+1)) 
     return $two 
    fi 
} 

function echoPoints() { 
    echo -e "$playerOne has $one points\n$playerTwo has $two points" 
} 

function progress() { 
    if [ "$one" -gt "$two" ] ; then 
      echo Winner is \$playerOne with \$one points. 
      exit 0 
    elif [ "$one" -lt "$two" ] ; then 
     echo Winner is \$playerTwo with \$two points. 
     exit 0 
    else 
     questions ~/TechnicalWritting/Random.txt 
    fi 
} 

function questions() { 
    line="$(wc -l $1 | awk '{print $1}')" 
    for ((x = 1 ; x <= 2 ; x++)) ; do 
     for ((i = 1 ; i <= "$line" ; i++)) ; do 
      read -p "$(head -$i $1 | tail -1 | awk -F _ '{print $1}')" ans 
      if [ "$ans" = "" ] ; then 
       echo 2&1 Sorry, wrong answer. 
      elif [ "$ans" = "$(head -$i $1 | tail -1 | awk -F _ '{ print $2 }'" ] ; then 
       echo Good job random fella. 
       points 
      else 
       echo 2&1 Sorry, wrong answer. 
      fi 
     done 
    done 
} 

questions ~/TechnicalWritting/Math.txt 
questions ~/TechnicalWritting/Logic.txt 
questions ~/TechnicalWritting/Funny.txt 
progress 
+1

我想你可以開始調試這個孤立的問題。例如,通過評論除第一行之外的所有行。檢查是否有效。然後取消第二個註釋。等等。 –

+0

'echo 2&1'不正確;它在後臺運行'echo 2',然後嘗試執行一個名爲'1'的命令。如果你正試圖向stderr發送消息,你需要'echo message1>&2'。此外,'回聲贏家是\ $ playerOne \ 1分.'從字面上打印'贏家是\ $ playerOne與\ $ 1分。它不擴展變量名稱。 –

+0

@KeithThompson我遵循了你的建議,仍然得到完全相同的錯誤信息。感謝您爲我解決這些問題。 –

回答

3

你的錯誤是在這一行:

elif [ "$ans" = "$(head -$i $1 | tail -1 | awk -F _ '{ print $2 }'" ] ; then 

$()缺少收盤)

elif [ "$ans" = "$(head -$i $1 | tail -1 | awk -F _ '{ print $2 }')" ] ; then 

建議:1.我認爲這是一個bash腳本。第一行應該是

#! /bin/bash 

2.使用語法高亮顯示的編輯器。我正在使用凱特,也有很多其他的。使用凱特,我直截了當地發現你的語法錯誤。

+0

非常感謝。我一直在看它4個小時,看不到它。我也有「#!/ bin/bash -u」,但是沒有把它寫在問題中。 我會看看凱特,因爲使用VIM和gedit並沒有幫助我看到。 –

+0

歡迎您!有時候,我的眼睛也有西紅柿,我很感謝每個人都向我展示了熱點。 :-)凱特很酷。它有很多QT和KDE依賴項,但它是值得的。 bash的語法高亮顯示在菜單中隱藏了一點,但是如果腳本以'#!開始的話,kate會自動使用它。 /斌/ bash'。我不確定,如果腳本也必須可執行。只有極少數情況下它不能正確顯示bash語法。 (單個'case'行以';;'立即以新的'case'行緊接着結尾) – mviereck

相關問題