意外标记`}'`附近的语法错误

时间:2016-02-29 01:03:24

标签: bash

我正在编写一个shell脚本,为游戏冒雨的风险制作新的保存文件。我正在尝试这样做,以便可以将所有可用的解锁部分(例如成就,怪物日志,工件等)一次性添加到文件中,或者用户将能够选择哪些部分他们想要补充。我是编写shell脚本的新手,我不太清楚为什么我在运行到目前为止的操作时遇到错误 语法错误接近意外令牌`}' 我的剧本如果有人能向我解释为什么我收到错误,如何解决它,和/或如何改进我的脚本,我将不胜感激。如果重要的话,我也在Mac OS X上。这是我的剧本。

{{1}}

2 个答案:

答案 0 :(得分:0)

您的脚本有一些错误: - 第93行:elif [ $text = "2" ] then;应更改为elif [ $text = "2" ]; then - 如果bash中的命令必须有fi来关闭条件。你可以参考http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html 希望这个帮助

答案 1 :(得分:-3)

在决定编写脚本之前,您应该对您正在编写的语言有基本的了解。即使是粗略阅读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