我对 bash 还很菜,我想尝试挑战自己,尝试制作一款选择你自己的冒险游戏。我开始尝试使用if
参数,但没有成功,所以我又尝试了case
,但似乎也没有成功!这是我的脚本:
#!/bin/bash
clear
echo "you are in a dark room."
echo "1: Turn on light"
echo "2: Go back to sleep"
read option1
case $option1 in
1) echo "The room fills with light, revealing itself. It is a
small room, with a Steel door and a small keyhole. There is a small
piggybank with a keyhole and a small wooden chest beside it without a
keyhole."
2) echo "What little of the room you can see fades as sleep
washes over you"
exit
esac
有人能告诉我我做错了什么吗?每当我尝试运行它时,它都会向我吐出以下内容:
/home/sqidman31/Desktop/Test_Folder/Story2.sh: line 10: syntax error
near unexpected token `)'
/home/sqidman31/Desktop/Test_Folder/Story2.sh: line 10: ` 2) echo
"What little of the room you can see fades as sleep washes over you"'
答案1
抱歉,伙计们,我搞砸了。我忘了为语句添加操作else
,结果搞砸了。误报。
答案2
您的case
构造格式不正确 - 每个案例都需要通过以下方式终止;;
:
#!/bin/bash
clear
echo "you are in a dark room."
echo "1: Turn on light"
echo "2: Go back to sleep"
read option1
case $option1 in
1) echo "The room fills with light, revealing itself. It is a
small room, with a Steel door and a small keyhole. There is a small
piggybank with a keyhole and a small wooden chest beside it without a
keyhole."
;;
2) echo "What little of the room you can see fades as sleep
washes over you"
;;
*)
exit
;;
esac