0

可能重复:
如果语句不遵循其条件

我认为这是我的 bc 或我的变量有问题,但我可能是错的。

这是我的一些代码。

#!/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 -eq 1 ]; do

    echo "roll or stay"
    read choice

if [ $choice ="r" ]; then

    die=$(($RANDOM%6+1))
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 [ $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 -gt 1 ]; then

    p1s=$(echo "$p1s+$die" |bc)
    count=$(echo "$count+$die" |bc)
    echo "You rolled a $die. Your score is $p1s"
    pt=1

fi


if [ $p1s -gt 99 ]; then

    echo "$p1 won. $p2 lost"
    echo "would you like to play again?"
    read again

else

if [ $again ="yes" ]; then

    echo "Okay one second."
    sleep 1
    clear
    bash num.sh

else

if [ $again = "no" ]; then

    exit
else

    echo "I guess no then?"
    exit
        fi
    fi
fi
done

发生的事情是这样的

玩家 1 的名字?

1

玩家2的名字?

2

好的1和2。1会先走

滚动或停留

r

num.sh: 第 24 行: [: r: 一元运算符预期

num.sh:第 42 行:[:-eq:预期一元运算符

num.sh: 第 51 行: [: -gt: 一元运算符预期

好的一秒钟。

玩家 1 的名字?

在这里它会发现一堆错误等等。然后重新开始。

先感谢您。

4

1 回答 1

0

我猜有条件检查选择变量有错误的语法,它应该写成:

if [ "$choice" -eq "r" ]; then
...

或带有增强的括号

if [[ "$choice" == "r" ]]; then
...
于 2012-11-09T21:09:08.910 回答