错误D:我已经尝试过很多从糟糕的elif到解析错误的问题。固定?

时间:2012-11-09 20:32:46

标签: bash shell

  

可能重复:
  If statement is not following its conditional

我认为这是我的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名?

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

提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

或使用增强的括号

if [[ "$choice" == "r" ]]; then
...