shell - 奇怪的语法问题(在语法正常的情况下获得意外的单词异常)

时间:2013-08-06 14:51:49

标签: shell unix

我目前正在处理服务器启动脚本。一切看起来都很好,但它不会执行所以我使用-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和复制粘贴了大部分代码,但就这一点而言,我明白了!而这真的让我心碎。我不知道为什么会这样。

P.S。:elif出现相同类型的错误。无论先发生什么!

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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