2016-02-29 130 views
1

我正在編寫一個shell腳本來爲遊戲下雨風險創建一個新的保存文件。我試圖讓每一個可用的解鎖部分,比如成就,怪物日誌,工件等都可以一次添加到文件中,或者用戶可以選擇哪些部分他們想要添加。我是新手,編寫shell腳本,我不太清楚爲什麼當我運行我迄今爲止的腳本時,出現錯誤語法錯誤附近的意外令牌`}'。如果任何人都可以向我解釋爲什麼我得到錯誤,如何解決它,和/或如何改進我的腳本,將不勝感激。如果有問題,我也在Mac OS X上。這是我的腳本。意外令牌附近的語法錯誤`}'

#!/bin/bash 
mkdir ~/Desktop/ror_save_unlock_all 

echo "This script takes ~/Library/Application Support/com.riskofrain.riskofrain/Save.ini and backs it up to ~/Desktop/ror_save_unlock_all/originalSave. It generates a new Save.ini that is built to your specifications, unlocking all items and achievements or just the ones you choose, and replaces the existing Save.ini with the new one." >> ~/Desktop/ror_save_unlock_all/README.txt 

mkdir ~/Desktop/ror_save_unlock_all/originalSave 

cp ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini ~/Desktop/ror_save_unlock_all/originalSave/ 

cd ~/Desktop/ror_save_unlock_all 

echo -n 'How would you like the new Save.ini to be built? Press 1 for all unlocks or 2 to customize.' 
read text 
if [ $text = "1" ] 
then 
    { 
     echo "[Achievement]" >> EOF1 

     count=1 

     while [ $count -lt 55 ] 
     do 
      echo "acheivement${count}=\"2\"" >> EOF2 
      count=`expr $count + 1` 
     done 

     echo "[Record]" >> EOF3 

     count=1 

     while [ $count -lt 31 ] 
     do 
      echo "mons${count}=\"1\"" >> EOF4 
      count=`expr $count + 1` 
     done 

     count=1 

     while [ $count -lt 110 ] 
     do 
      echo "item${count}=\"1\"" >> EOF5 
      count=`expr $count + 1` 
     done 

     count=1 

     while [ $count -lt 10 ] 
     do 
      echo "artifact${count}=\"1\"" >> EOF6 
      count=`expr $count + 1` 
     done 

     cat EOF1 EOF2 EOF3 EOF4 EOF5 EOF6 > Save.ini 

     rm EOF1 EOF2 EOF3 EOF4 EOF5 EOF6 

     cp -force ~/Desktop/ror_save_unlock_all/Save.ini ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini 

     echo "Original Save.ini successfully overwritten." 

     exit 

    } 

elif [ $text = "2" ]; then 
    { 
     echo "You selected customize. Now we will build a custom Save.ini" 
     echo -n "Press 1 if you want to unlock all achievements, press 2 to continue without unlocking all achievements." 
     read text 
     if [ $text = "1" ]; then 

      { 
       echo "[Achievement]" >> EOF1 
       count=1 
       while [ $count -lt 55 ] 
       do 
        echo "acheivement${count}=\"2\"" >> EOF2 
        count=`expr $count + 1` 
       done 

       echo "All achievements successfully unlocked." 
       echo -n "Press 1 to make the Save.ini and replace the existing one with it and then exit, or press 2 to customize it further." 
       read text 
       if [ $text = "1" ]; then 
        { 
         cat EOF1 EOF2 > Save.ini 
         rm EOF1 EOF2 
         cp -force ~/Desktop/ror_save_unlock_all/Save.ini ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini 
         echo "Original Save.ini successfully overwritten." 
         exit 
        } 

       elif [ $text = "2" ] then; 
        { 
         echo -n "Press 1 to unlock all monster logs, or press 2 to continue without unlocking all monster logs." 
         read text 
         if [ $text = "1" ]; then 
          { 
           echo "[Record]" >> EOF3 

           count=1 
           while [ $count -lt 31 ] 
           do 
            echo "mons${count}=\"1\"" >> EOF4 
            count=`expr $count + 1` 
           done 

           echo "All achievements successfully unlocked." 
           echo -n "Press 1 to make the Save.ini and replace the existing one with it and then exit, or press 2 to customize it further." 
           read text 
          } 
         } 
        } 
       } 
+0

你有一個不平衡的'}'。解決這個問題的最佳方法是縮進你的代碼,或者使用編輯器來支持你的bash(比如VIM) –

+4

這些大括號都不是必須的。 – chepner

+0

在shell中,大括號用於分組I/O重定向,通常不僅用於組命令。你最後有一串緊密的括號;四個中的第二個應該是「fi」,或者需要一個「fi」。您至少缺少兩個腳本中的「fi」。我會讓你理清你想要保留的大括號(它們都不是必須的) - 但是你的腳本必須在沒有它們的情況下才有意義。 –

回答

0

在決定寫一個腳本之前,你應該對你寫的語言有基本的瞭解。即使是最粗略的讀數the documentation或任何how-to pages都會告訴您if語句的正確語法。

除此之外,您的腳本中存在許多低效率問題。反引號操作符啓動一個子shell,一個新的實例bash,所以這不一定是你想要在循環中做110次的東西。

下面是一些固定的大問題。我建議看看functions以消除大量的代碼重複和條件嵌套。

#!/bin/bash 
mkdir ~/Desktop/ror_save_unlock_all 

echo "This script takes ~/Library/Application Support/com.riskofrain.riskofrain/Save.ini and backs it up to ~/Desktop/ror_save_unlock_all/originalSave. It generates a new Save.ini that is built to your specifications, unlocking all items and achievements or just the ones you choose, and replaces the existing Save.ini with the new one." >> ~/Desktop/ror_save_unlock_all/README.txt 

mkdir ~/Desktop/ror_save_unlock_all/originalSave 

cp ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini ~/Desktop/ror_save_unlock_all/originalSave/ 

cd ~/Desktop/ror_save_unlock_all 

read -n 1 -p 'How would you like the new Save.ini to be built? Press 1 for all unlocks or 2 to customize.' text 
if [ $text = "1" ] 
    then 
    echo "[Achievement]" > Save.ini 
    for count in {1..54}; do 
     echo "acheivement${count}=\"2\"" >> Save.ini 
    done 

    echo "[Record]" >> Save.ini 
    for count in {1..30}; do 
     echo "mons${count}=\"1\"" >> Save.ini 
    done 

    for count in {1..109}; do 
     echo "item${count}=\"1\"" >> Save.ini 
    done 

    for count in {1..9}; do 
     echo "artifact${count}=\"1\"" >> Save.ini 
    done 

    cp -force ~/Desktop/ror_save_unlock_all/Save.ini ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini 
    echo "Original Save.ini successfully overwritten." 
    exit 

elif [ $text = "2" ]; then 
    echo "You selected customize. Now we will build a custom Save.ini" 
    read -n 1 -p "Press 1 if you want to unlock all achievements, press 2 to continue without unlocking all achievements." text 
    if [ $text = "1" ]; then 
     echo "[Achievement]" > Save.ini 
     for count in {1..54}; do 
      echo "acheivement${count}=\"2\"" >> Save.ini 
     done 

     echo "All achievements successfully unlocked." 
     read -n 1 -p "Press 1 to make the Save.ini and replace the existing one with it and then exit, or press 2 to customize it further." text 
     if [ $text = "1" ]; then 
      cp -force ~/Desktop/ror_save_unlock_all/Save.ini ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini 
      echo "Original Save.ini successfully overwritten." 
      exit 

     elif [ $text = "2" ]; then 
      read -n 1 -p "Press 1 to unlock all monster logs, or press 2 to continue without unlocking all monster logs." text 
      if [ $text = "1" ]; then 
       echo "[Record]" >> Save.ini 

       for count in {1..30}; do 
       echo "mons${count}=\"1\"" >> Save.ini 
       done 

       echo "All monster logs successfully unlocked." 
       read -n 1 -p "Press 1 to unlock all monster logs, or press 2 to continue without unlocking all monster logs." text 
       if [ $text = "1" ]; then 
        #... 
       fi 
      fi 
     fi 
    fi 
fi 
相關問題