BASH 脚本期待然后,当我需要其他 [英] BASH script expecting then, when I need else
问题描述
我的 bash 脚本需要一点帮助:
I need a little help with my bash script:
#!/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
这是一个脚本,用于启动我拥有的一些 Minecraft 服务器.我最终也会为所有其他服务器添加 if 条目.这是我不选择 Creative+Survival 时的输出:
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:
Server Startup - INCOMPLETE.sh: 20: Server Startup - INCOMPLETE.sh: Syntax error: "else" unexpected (expecting "then")
当我选择 Creative+Survival 时,也会发生同样的事情.对不起,如果这是一个愚蠢的问题,这是我的第一个 bash 脚本.
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.
谢谢!
推荐答案
缺少括号 [
(测试)运算符.应该是这样的:
The bracket [
(test) operator is missing. It should be something like this:
if [ "$server" = "Creative+Survival" ]
then
echo "true"
else
echo "false"
fi
注意:这些括号周围的空格很重要.条件测试周围的方括号实际上是 test
运算符的同义词.所以上面的等价于:
NOTE: the spaces around those brackets are important.
The square brackets around the conditional test are actually a synonym for the test
operator.
So the above is equivalent to:
if test "$server" = "Creative+Survival"
then
echo "true"
else
echo "false"
fi
但是大家都用括号;我很少看到使用 test
关键字的脚本.
But everyone uses the brackets; I rarely see scripts that use the test
keyword.
在 bash shell 中还允许使用双括号(尽管这不是可移植的,因为它不是 POSIX 标准):
Also allowed in the bash shell (although this is not as portable because it is not a POSIX standard) is to use double brackets:
if [[ "$server" = "Creative+Survival" ]]
then
这是一个页面链接,描述了 [
、[[
和 test
之间的区别:http://mywiki.wooledge.org/BashFAQ/031
Here's a link to page that describes the differences between [
, [[
, and test
:
http://mywiki.wooledge.org/BashFAQ/031
更新
问:这似乎可行,但如果我选择Creative+Survival"以外的任何其他选项,它会吐出错误代码.这是应该发生的吗?
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?
答:根本不清楚哪个组件正在吐出什么错误代码.我希望您想检查每个可能的选择.您可以使用 elif
或 case
来做到这一点.
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
.
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
(注意:缩进只是为了提高可读性;bash 关心换行符,而不是前导空格.)
(NOTE: the indentation is to improve readability only; bash cares about the newlines, not the leading spaces.)
这篇关于BASH 脚本期待然后,当我需要其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!