#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
Bokoblin_HP=30
Bokoblin_STR=5
Link_HP=60
Link_STR=10
while [ $Bokoblin_HP -ne 0 ]
echo "Bokblin HP : ${Bokoblin_HP}/30"
do
read -p "Press A to Attack or Press H to Heal : " action
if [ $action = "A" ]
then
((Bokoblin_HP=$Bokoblin_HP-$Link_STR))
echo $Bokoblin_HP
fi
done
结果:
Bokoblin HP : 30/30
Press A to Attack or Press H to Heal : A
30
我正在对Bokoblin_HP进行操作,我想在该程序达到0时停止该程序,但是我的变量不会更改,并且仍然是30。
答案 0 :(得分:1)
只需交换两行即可使程序正常工作。
此外,请务必键入上方的A
#!/bin/bash
RED='\033[0;31m'
NC='\033[0m'
Bokoblin_HP=30
Bokoblin_STR=5
Link_HP=60
Link_STR=10
while [ $Bokoblin_HP -ne 0 ]
do
echo "Bokblin HP : ${Bokoblin_HP}/30"
read -p "Press A to Attack or Press H to Heal : " action
if [ $action = "A" ]
then
((Bokoblin_HP=$Bokoblin_HP-$Link_STR))
echo $Bokoblin_HP
fi
done
输出:
<user>@<machine> ~
└─ $ ./test.sh
Bokblin HP : 30/30
Press A to Attack or Press H to Heal : A
20
Bokblin HP : 20/30
Press A to Attack or Press H to Heal : A
10
Bokblin HP : 10/30
Press A to Attack or Press H to Heal : A
0
<user>@<machine> ~
答案 1 :(得分:0)
Idk应该如何使用您的解决方案,
Bokoblin_HP=$(($Bokoblin_HP-$Link_STR))