BASH 脚本期待然后,当我需要其他

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14073294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:06:25  来源:igfitidea点击:

BASH script expecting then, when I need else

bashif-statementsyntax-error

提问by JonaK

I need a little help with my bash script:

我的 bash 脚本需要一些帮助:

#!/bin/bash

zenity --list --title="Select Server" --text="Select the server to start" --radiolist \
--column= --column=Server --column=Port \
FALSE "Creative+Survival" 25565 \
FALSE "Tekkit Cheat Survival" 25566 \
FALSE "Flat Tekkit" 25567 \
FALSE "SunnyDale City" 25568 \
FALSE "Doom Dungeon" 25569 \
FALSE "Survival Tekkit" 25570 \
| cat > ~/temp


server=$(cat ~/temp)

if $server="Creative+Survival" then
gnome-terminal -x sh "/home/kulboy121/Servers/Creative and Survival/launch.sh"
end

else
echo wrong answer...
end

rm ~/temp

This is a script to launch some Minecraft servers I own. I will eventually add if entries for all the other servers as well. This is the output when I do not select Creative+Survival:

这是用于启动我拥有的一些 Minecraft 服务器的脚本。我最终也会为所有其他服务器添加 if 条目。这是当我不选择 Creative+Survival 时的输出:

Server Startup - INCOMPLETE.sh: 20: Server Startup - INCOMPLETE.sh: Syntax error: "else" unexpected (expecting "then")

And when I do select Creative+Survival, the same thing happens. Sorry if this is a stupid question, this is one of my first bash scripts.

当我选择 Creative+Survival 时,也会发生同样的事情。对不起,如果这是一个愚蠢的问题,这是我的第一个 bash 脚本之一。

Thank you!

谢谢!

采纳答案by spencer7593

The bracket [(test) operator is missing. It should be something like this:

[缺少括号(测试)运算符。它应该是这样的:

if [ "$server" = "Creative+Survival" ]
then
    echo "true"
else
    echo "false"
fi

NOTE: the spaces around those brackets are important. The square brackets around the conditional test are actually a synonym for the testoperator. So the above is equivalent to:

注意:这些括号周围的空格很重要。条件测试周围的方括号实际上是test运算符的同义词。所以上面的等价于:

if test "$server" = "Creative+Survival"
then
   echo "true"
else
   echo "false"
fi

But everyone uses the brackets; I rarely see scripts that use the testkeyword.

但是每个人都使用括号;我很少看到使用test关键字的脚本。

Also allowed in the bash shell (although this is not as portable because it is not a POSIX standard) is to use double brackets:

在 bash shell 中还允许使用双括号(尽管这不是可移植的,因为它不是 POSIX 标准):

if [[ "$server" = "Creative+Survival" ]]
then

Here's a link to page that describes the differences between [, [[, and test: http://mywiki.wooledge.org/BashFAQ/031

下面是页面的链接描述之间的差异[[[以及testhttp://mywiki.wooledge.org/BashFAQ/031



Update

更新

Q:This seems to work, but it spits out an error code if I select anything other then "Creative+Survival". Is this supposed to happen?

问:这似乎有效,但如果我选择“Creative+Survival”以外的任何内容,它会吐出错误代码。这是应该发生的吗?

A:It's not at all clear what error code is being spit out by what component. I expect you want to check for each possible selection. You can do that with an elif, or with a case.

A:具体是哪个组件吐出什么错误代码,完全不清楚。我希望您想检查每个可能的选择。您可以使用elif, 或使用case.

if [ "$server" = "Creative+Survival" ]
then
    echo "Creative and Survival"
elif [ "$server" = "Tekkit Cheat Survival" ]
then
    echo "Tekkit Cheat Survival"
elif [ "$server" = "Flat Tekkit" ]
then
    echo "Flat Tekkit"
else
    echo "no action for specified server"
fi


case "$server" in
    'Creative+Survival')
        echo "Creative and Survival"
        ;;
    'Tekkit Cheat Survival')
        echo "Tekkit Cheat Survival"
        ;;
    *)
        echo "no action found for server $server"
        ;;
esac

(NOTE: the indentation is to improve readability only; bash cares about the newlines, not the leading spaces.)

(注意:缩进只是为了提高可读性;bash 关心换行符,而不是前导空格。)

回答by Adam Sznajder

In bash you have to remember that thenmust be in a new line. Unfortunately your whole ifstatement is not correct. The proper string comparison looks something like that:

在 bash 中,您必须记住它then必须在一个新行中。不幸的是,你的整个if陈述是不正确的。正确的字符串比较看起来像这样:

 if [[ "$server" = "pattern" ]]; then
       # do something
 else
       # do something else
 fi

回答by dogbane

Your if-statement is incorrect.

您的 if 语句不正确。

  • The condition goes within brackets: [[...]]
  • thengoes on a new line
  • The statement ends with finot end
  • 条件在括号内: [[...]]
  • then换了一条线
  • 语句以finot结尾end

There is also a Useless Use of catwhich I have fixed.

还有一个我已经修复的无用用途cat

Try this:

尝试这个:

server=$(zenity --list --title="Select Server" --text="Select the server to start" --radiolist \
--column= --column=Server --column=Port \
FALSE "Creative+Survival" 25565 \
FALSE "Tekkit Cheat Survival" 25566 \
FALSE "Flat Tekkit" 25567 \
FALSE "SunnyDale City" 25568 \
FALSE "Doom Dungeon" 25569 \
FALSE "Survival Tekkit" 25570)
if [[ $server == "Creative+Survival" ]]
then
    gnome-terminal -x sh "/home/kulboy121/Servers/Creative and Survival/launch.sh"
else
    echo wrong answer...
fi

回答by glenn Hymanman

Others have answered the syntax problems.

其他人已经回答了语法问题。

Going forward, the casestatement will serve you well:

展望未来,该case声明将为您提供良好的服务:

server=$(...)
case $server in 
  "Creative+Survival")
    gnome-terminal -x sh "/home/kulboy121/Servers/Creative and Survival/launch.sh"
    ;;
  "Tekkit Cheat Survival")
    # do something
    ;;
  "Flat Tekkit")
    # do something
    ;;
  "SunnyDale City")
    # do something
    ;;
  "Doom Dungeon")
    # do something
    ;;
  "Survival Tekkit")
    # do something
    ;;
  *)
    echo wrong answer
    ;;
esac

Additionally, rather than use zenity, use bash's selectstatement. Assuming you have bash v4

此外,不要使用 zenity,而是使用 bash 的select语句。假设你有 bash v4

declare -A port
port["Creative+Survival"]=25565
port["Tekkit Cheat Survival"]=25566
port["Flat Tekkit"]=25567
port["SunnyDale City"]=25568
port["Doom Dungeon"]=25569
port["Survival Tekkit"]=25570

PS3="Select server: "
select server in "${!port[@]}"; do
    if [[ -n $server ]]; then
        # user has given a valid selection
        echo do something with "$server" and "${port[$server]}"
        break
    fi
done