当它运行时,它会跳过IF,而无视我的条件.为什么和修复

  • 本文关键字:条件 运行时 IF bash shell
  • 更新时间 :
  • 英文 :


我的代码在这里。不过,您不必担心第二个。它完全相同,只是播放器2。

#!/bin/bash
#this is a game that is two player and it is a race to get to 
#100 before the other player
echo "Player 1 name?"
read p1
echo "Player 2 name?"
read p2
echo "Okay $p1 and $p2. $p1 will go first"
p1s=0
p2s=0
pt=1
while [ $pt == 1 ]; do
echo "roll or stay"
read choice
if [ $choice == r ]; then
die=$(($RANDOM%6+1))
elif [ $die -eq 1 ]; then
p1s=$(echo "$p1s-$count" |bc)
echo "You rolled a 1. Your score is $p1s"
echo "$p2 turn now."
sleep 1
count=0
pt=2
elif [ $die > 1 ]; then
p1s=$(echo "$p1s+$die" |bc)
count=$(echo "$count+$die" |bc)
echo "You rolled a $die. Your score is $p1s"
pt=1
else
if [ $choice == s ]; then
echo "Okay $p1 your score is $p1s"
echo "$p2 turn now"
sleep 1
count=0
pt=2
else
pt=1
fi
fi
if [ $p1s > 99 ]; then
echo "$p1 won. $p2 lost"
echo "would you like to play again?"
read again
elif [ $again == yes ]; then
echo "Okay one second."
sleep 1
clear
bash num.sh
elif [ $again == no ]; then
echo "ok going back to the games directory then"
sleep 1
bash games.sh
fi
done
while [ $pt == 2 ]; do
echo "roll or stay"
read choice
if [ $choice == r ]; then
die=$(($RANDOM%6+1))
elif [ $die -eq 1 ]; then
p1s=$(echo "$p2s-$count" |bc)
echo "You rolled a 1. Your score is $p2s"
echo "$p1 turn now."
sleep 1
count=0
pt=2
elif [ $die > 1 ]; then
p1s=$(echo "$p2s+$die" |bc)
count=$(echo "$count+$die" |bc)
echo "You rolled a $die. Your score is $p2s"
pt=1
else
if [ $choice == s ]; then
echo "Okay $p1 your score is $p2s"
echo "$p1 turn now"
sleep 1
count=0
pt=2
else
pt=2
fi
fi
if [ $p2s > 99 ]; then
echo "$p2 won. $p1 lost"
echo "would you like to play again?"
read again
elif [ $again == yes ]; then
echo "Okay one second."
sleep 1
clear
bash num.sh
elif [ $again == no ]; then
echo "ok going back to the games directory then"
sleep 1
bash games.sh
fi
done

我运行后会发生什么

Player 1 name?
name1
Player 2 name?
name2
Okay name1 and name2. name1 will go first
roll or stay
r
name1 won. name2 lost
would you like to play again?

它应该滚动,如果一个随机数,则该回合的分数将被删除,然后转到下一个球员。如果您滚动2-6,则会增加得分。如果您留下来,那么您就可以节省得分,并且每回合得分加起来,直到一个球员获得100分。

您用于测试数字关系的测试比较运算符是错误的。例如:

elif [ $die > 1 ]; then应该是----> elif [ $die -gt 1 ]; then

if [ $p2s > 99 ]; then应该是----> if [ $p2s -gt 99 ]; then

等等。

这是数字值的测试比较运算符的列表:

  • == ---> -eq(等于)
  • != ---> -ne(不是平等)
  • > ---> -gt(大于)
  • < ---> -lt(小于)
  • >= ---> -ge(大或等于)
  • <= ---> -le(较小或等于)

相关内容

  • 没有找到相关文章

最新更新