0

Тhis is the first script I've ever written and I'm looking for some help. I can't find a script like the one I'm trying to write and its becoming a bother because I wish to create more/expand upon such a bash structure.

If some one has the time could they maybe proof read this for me. Could someone tell me what am I doing wrong and why?

#! /bin/bash
#  script to turn the screen blue
echo -e '\033[1;32m'
echo "Minecraft Server LTG Bukkit 1.4.7"
echo "Minecraft Server LTG Bukkit 1.4.7"
echo -e '\033[31m' "[Start  ] \033[1;32m LTG MineCraft Server"
echo -e '\033[31m' "[Stop   ] \033[1;32m LTG MineCraft Server"
echo -e '\033[31m' "[Restart] \033[1;32m LTG MineCraft Server"
echo -e '\033[0m'
cho -e "Hello, \033[47m \033[30m"$USER" \033[0m Enter Command:"

echo  "Command:"
read  $COM1 start stop restart

if ["$COM1" = "start"]:  then
       echo "ran minecraft serv"
     if [ "$(pgrep -g java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar)" ] ; then
       echo MineCraft Server Bukkit 1.4.7 L.T.G : Running

     else
       echo -e "\033[1;32m MineCraft Server Bukkit 1.4.7 L.T.G : \033[31m  FAILED \033[0m"
     fi
if ["$COM1" "stop"]: then
    echo -e "\033[1;32m  MineCraft Server Bukkit 1.4.7 L.T.G : Shutting Down \033[0m"
    killall java
     if (( "$(pgrep -g java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar)" )) ; then
    killall java
    echo -e "\033[1;32m MineCraft Server Bukkit 1.4.7 L.T.G: is SHUTDOWN \033[0m"
     fi
if [ "$COM1"  "Restart" ] ; then
     echo MineCraft Server Bukkit 1.4.7 L.T.G : Rebooting
     exit 1
 fi
fi
exit 0
4

2 回答 2

3

让我们走两条线......那里足以让我们忙碌。

read  $COM1 start stop restart

if ["$COM1" = "start"]:  then

第一行读入了一个变量,其名称被存储在$COM1其中(它是未初始化的,所以实际上它是空的,所以它毕竟没有做任何事情),加上三个变量start,stoprestart。您要么需要:

read COM1 start stop restart

或者您需要COM1在使用它之前进行初始化。

第二行设法遇到了数量惊人的问题。

  1. test命令 aka[是命令名称,而不是符号。因此,它需要与其论点分开。请注意,通常有一个 command/bin/[/usr/bin/[,尽管它现在也是一个内置的 shell。
  2. 幸运的是,既然$COM1是未设置和空的,它已经被分开了,但更多的是偶然而不是设计。
  3. 因此,您的[命令使用 3 个参数执行=]:then. 这不是对 的有效调用[。最后一个论点应该是]独立的。
  4. 您应该使用分号将 与]分开then

总的来说,你应该写:

if [ "$COM1" = "start" ]
then

或者您可以添加一个分号(不必与]空格分隔),然后添加then

if [ "$COM1" = "start" ]; then

从风格上讲,你Minecraft Server LTG Bukkit 1.4.7在整个地方都重复了字符串'';不!使用变量来保存它。

担心您的嵌入式光标控制序列;不同的终端有不同的序列,因此您仅限于单一终端类型。修复更难;调查tput命令。

于 2013-01-22T07:30:12.740 回答
0

好吧,这就是我最终得到的结果-它有效,但我想知道是否有办法改善这种灾难,我真的很感谢您的回应:)

#! /bin/bash
#  script to turn the screen blue
NAME=$(echo Minecraft Server LTG Bukkit 1.4.7)
echo -e '\033[1;32m'
echo "$NAME"
echo "$NAME"
N1=$(echo LTG MineCraft Server)
echo -e '\033[31m' "[Start  ] \033[1;32m $N1"
echo -e '\033[31m' "[Stop   ] \033[1;32m $N1"
echo -e '\033[31m' "[Restart] \033[1;32m $N1"
echo -e '\033[0m'
echo -e "Hello \033[47m \033[30m"$USER" \033[0m Enter Command:"

echo  "Command:"
read test
#this is a test piece
echo "start = $test"

if [ $test == "start" ];  then
echo -e "\033[31m $NAME : Starting"
java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar &>./minecraft.sh.rlog
  if [ "$(pgrep -g java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar)" ] ; then
       echo -e "\033[31m $NAME : Running"
       exit 1
  else
    echo -e "\033[1;32m $NAME : \033[31m  FAILED \033[0m"
  fi
fi
if [ $test == "stop" ]; then
    echo -e "\033[1;32m $NAME : Shutting Down \033[0m"
        killall java &> ./minecraft.sh.log
        MINECRAFT=$(pgrep -g java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar                             &>./minecraft.sh.log)
                sleep 1
                echo .
                sleep 1
                echo .
                sleep 1
                echo .
                sleep 1
                echo .
     if [[ -z "$MINECRAFT" ]] ; then
        killall java &> ./minecraft.sh.log
        echo -e "\033[1;32m $NAME : IS SHUTDOWN \033[0m"
     else
         #should never bee seen
        echo Something Went Wrong
     fi
fi
if [ $test == "restart" ] ; then
     echo -e "\033[31m $NAME : Rebooting \033[0m"
        kill java &>minecraft.sh.log
        sleep 1
 echo .
        sleep 1
        echo .
        if [[ -z "$MINECAFT" ]]; then
        echo -e "\033[1;32m $NAME : IS SHUTDOWN \033[0m"
        sleep 1
        echo .
        fi
        echo -e "\033[31m $NAME : Starting \033[0m"
        sleep 1
        echo .
         java -Xmx256M -Xms256M -jar /minecraft/minecraft_server.jar &> ./minecraft.sh.rlog
else
if [ $test == "exit" ] ; then
        exit 0
fi

exit 1
fi
于 2013-01-23T05:15:40.517 回答