0

我目前正在研究服务器启动脚本。一切看起来都很好,但它不会执行,所以我使用 -n 标志检查了语法。我给了我一个错误:

Syntax error: word unexpected (expecting "in")

这真的很奇怪,因为在正确的地方!

这是行:

            case $vtyp in

这是整个代码:

#!/bin/bash
BACKUP_PATH="$HOME/backups"
MINECRAFT_PATH="$HOME/server"
SCREEN_NAME="brain"
SERVER_JAR="minecraft.jar"
SERVER_JAR_PATH="$MINECRAFT_PATH/$SERVER_JAR"
RAM="Xmx1G";
MESSAGE_BACKUP="Backup wird erstellt..."
MESSAGE_STARTING="Server wird gestartet..."
MESSAGE_ALREADY_STARTED="Server bereits gestartet, wechsle zur Konsole..."
MESSAGE_NOT_STARTED="Fehler, nicht gestartet..."
MESSAGE_UNKNOWN="Unbekanntes Argument $1"
MESSAGE_INVALID_VERSION="Unbekannter Versionstyp."
MESSAGE_VERSIONS="Bitte Version auswählen:
1 = Stable - Stabile Version
2 = Beta - Betaversion
3 = Dev - Entwicklungsversion
"
MESSAGE_VERSION_TYPES="Versionstypen: >>stable<< - Stabile Version >>beta<< - Betaversion >>dev<< - Entwicklungsversion"

URL_JAR_STABLE=""
URL_JAR_BETA=""
URL_JAR_DEV=""

DO_BACKUP()
{
    cd $HOME
    echo $MESSAGE_BACKUP
    mkdir $BACKUP_PATH
    date=$(date '+%Y-%m-%d %H-%M-%S')

    tar -cjvf "$BACKUP_PATH/Complete Backup $date.tar.bz2" $MINECRAFT_PATH

    rm -rf "$MINECRAFT_PATH/server.log backups/*"
}

DO_START()
{
    cd $MINECRAFT_PATH
    echo $MESSAGE_STARTING
    screen  -A -m -d -S $SCREEN_NAME java -$RAM -jar $SERVER_JAR_PATH -o true
}

DO_REATTACH()
{
    echo $MESSAGE_ALREADY_STARTED
    screen -d -r $SCREEN_NAME
}

DO_KILL()
{
    if [ screen -list | grep -q $SCREEN_NAME ]
    then
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say Server wird gestoppt.\r"`"
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 30\r"`"
        sleep 10
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 20\r"`"
        sleep 5
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 15\r"`"
        sleep 5
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 10\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 9\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 8\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 7\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 6\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 5\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 4\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 3\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 2\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say 1\r"`"
        sleep 1
        screen -S $SCREEN_NAME -p 0 -X stuff "`printf "stop\r"`"

        screen -d -r $SCREEN_NAME
    else
        echo $MESSAGE_NOT_STARTED
    fi
}

DO_NORMAL()
{
    if [ ! screen -list | grep -q $SCREEN_NAME ]
    then
        if [ ! -f $SERVER_JAR_PATH ]
        then
            DO_UPDATE
            DO_NORMAL
        else
            DO_BACKUP
            DO_START
        fi
    else
        DO_REATTACH
    fi
}

DO_UPDATE()
{
    if [ $# -lt 1 ]
    then
        while [ true ]
        do
            read -p "$MESSAGE_VERSIONS" vtyp

            case $vtyp in
                [1]*) DO_UPDATE "stable"; break;;
                [2]*) DO_UPDATE "beta"; break;;
                [3]*) DO_UPDATE "dev"; break;;
                * ) echo $MESSAGE_INVALID_VERSION;;
            esac
        done
    else
        if [ $1 = "stable" ]
        then
            mkdir $MINECRAFT_PATH
            wget $URL_JAR_STABLE -O "$SERVER_JAR_PATH"
        elif [ $1 = "beta" ]
        then
            mkdir $MINECRAFT_PATH
            wget $URL_JAR_BETA -O "$SERVER_JAR_PATH"
        elif [ $1 = "dev" ]
        then
            mkdir $MINECRAFT_PATH
            wget $URL_JAR_DEV -O "$SERVER_JAR_PATH"
        else

        echo $MESSAGE_INVALID_VERSION
        echo $MESSAGE_VERSION_TYPES
        fi
    fi
}

if [ $# -lt 1 ]
then
    DO_NORMAL
else
    if [ $1 = "backup" ]
    then
        if screen -list | grep -q $SCREEN_NAME;
        then
            DO_KILL
            DO_BACKUP
            DO_START
        else
            DO_BACKUP
        fi
    elif [ $1 = "kill" ]
    then
        DO_KILL
    elif [ $1 = "update" ]
    then
        DO_KILL
        DO_UPDATE $2
    else
        echo $MESSAGE_UNKNOWN
    fi
fi

有人知道为什么会这样吗?我对 shell 很陌生,并复制粘贴了大部分代码,但就这一点而言,我确实理解它!这真的让我心碎。我不知道为什么会这样。

PS:同样类型的错误发生在elif. 什么都先到!

4

3 回答 3

2

不是答案,而是代码审查:我会重写 DO_UPDATE 函数:

第一的

MESSAGE_VERSION_PROMPT="Bitte Version auswählen: "
MESSAGE_VERSIONS=(
    "Stable - Stabile Version"
    "Beta - Betaversion"
    "Dev - Entwicklungsversion"
)

然后

DO_UPDATE()
{
    if (( $# == 0 )); then
        local PS3="$MESSAGE_VERSION_PROMPT"
        select choice in "${MESSAGE_VERSIONS[@]}"; do
            case $REPLY in
                1) set -- stable; break ;;
                2) set -- beta  ; break ;;
                3) set -- dev   ; break ;;
            esac
        done
    fi
    case $1 in
        stable)
            mkdir $MINECRAFT_PATH
            wget $URL_JAR_STABLE -O "$SERVER_JAR_PATH"
            ;;
        beta)
            mkdir $MINECRAFT_PATH
            wget $URL_JAR_BETA -O "$SERVER_JAR_PATH"
            ;;
        dev)
            mkdir $MINECRAFT_PATH
            wget $URL_JAR_DEV -O "$SERVER_JAR_PATH"
            ;;
        *) 
            echo $MESSAGE_INVALID_VERSION
            echo $MESSAGE_VERSION_TYPES
            return 1
            ;;
    esac
}
于 2013-08-06T15:43:53.850 回答
1

也许这样做会有所帮助:在 PSPad -- 格式 --> Unix http://forum.pspad.com/read.php?2,52821,52831

于 2013-08-06T15:24:48.427 回答
0

您是否从 Word 或记事本文件中复制粘贴?尝试使用dos2unix

于 2013-08-06T15:18:43.070 回答