2012-11-09 26 views
0

可能重複:
If statement is not following its conditional錯誤D:我已經嘗試了很多從錯誤的elif中解析錯誤到現在。固定?

我相信它是東西是錯誤與我的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名?

玩家2的名字?

好1和2 1將去第一

輥或留

ř

num.sh:線24:[R:一元運算符預期

num.sh:line 42:[:-eq:unary operator expected

num .sh:第51行:[:-gt:一元運算符預期

好的一秒鐘。

玩家1的名字?

在這裏它發現了一堆錯誤等。然後重新開始。

預先感謝您。

+0

你就可以「看」到哪裏如果打開shell跟蹤功能,錯誤是使用'set + x'或'set -vx'。如果你想追蹤一小部分,然後用'set + vx'關閉它。 – shellter

回答

0

我想有條件的檢查選擇變量錯誤的語法,應該寫成:

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

或具有增強支架

if [[ "$choice" == "r" ]]; then 
... 
相關問題