Como continuar executando um programa no bash

0

Eu fiz um programa bash no Ubuntu. O programa é "rock, papel de tesoura com computador". Eu quero continuar este programa até a mensagem "você ganha, tchau tchau".

Alguém poderia sugerir um método para eu fazer isso usando o código abaixo?

#!/bin/bash

echo "Welcome to play Rock, Paper, Scissors"
echo "Choose Rock(0), Paper(1), Scissors(2)"

#read players choice
read PLAYER

choices=( 0 1 2 )

#randomize the computer choice
COMPUTER=${choices[$((RANDOM%3))]}

if [[ $PLAYER = 0 && $COMPUTER = 1 ]]
    then echo "i have paper , i win";
elif [[ $PLAYER = 1 && $COMPUTER = 1 ]]
    then echo "i have paper , we draw";
elif [[ $PLAYER = 2 && $COMPUTER = 1 ]]
    then echo "i have paper , you win, bye bye";
elif [[ $PLAYER = 0 && $COMPUTER = 0 ]]
    then echo "i have rock , we draw";
elif [[ $PLAYER = 1 && $COMPUTER = 0 ]]
    then echo "i have rock , you win, bye bye";
elif [[ $PLAYER = 2 && $COMPUTER = 0 ]]
    then echo "i have rock , i win";
elif [[ $PLAYER = 0 && $COMPUTER = 2 ]]
    then echo "i have scissors, you win, bye bye";
elif [[$count = 0 && $PLAYER = 1 && $COMPUTER = 2 ]]
    then echo "i have scissors, i win";
elif [[$count = 0 && $PLAYER = 2 && $COMPUTER = 2 ]]
    then echo "i have scissors, we draw "
fi
    
por leebyeoungju 30.04.2014 / 10:10

2 respostas

0

Encerre a coisa toda em um loop while true e break em cada ponto em que você deseja terminar.

Note também que não há necessidade da matriz choices .

#!/bin/bash


echo "Welcome to play Rock, Paper, Scissors"

while true
do
       echo "Choose Rock(0), Paper(1), Scissors(2)"
       #read players choice
       read PLAYER

       #randomize the computer choice

       COMPUTER=$((RANDOM%3))

       if [[ $PLAYER = 0 && $COMPUTER = 1 ]]
           then echo "i have paper , i win";
       elif [[ $PLAYER = 1 && $COMPUTER = 1 ]]
           then echo "i have paper , we draw";
       elif [[ $PLAYER = 2 && $COMPUTER = 1 ]]
           then echo "i have paper , you win, bye bye"; break
       elif [[ $PLAYER = 0 && $COMPUTER = 0 ]]
           then echo "i have rock , we draw";
       elif [[ $PLAYER = 1 && $COMPUTER = 0 ]]
           then echo "i have rock , you win, bye bye"; break
       elif [[ $PLAYER = 2 && $COMPUTER = 0 ]]
           then echo "i have rock , i win";
       elif [[ $PLAYER = 0 && $COMPUTER = 2 ]]
           then echo "i have scissors, you win, bye bye"; break
       elif [[$count = 0 && $PLAYER = 1 && $COMPUTER = 2 ]]
           then echo "i have scissors, i win";
       elif [[$count = 0 && $PLAYER = 2 && $COMPUTER = 2 ]]
           then echo "i have scissors, we draw "
       fi
done
    
por 30.04.2014 / 11:29
2

Você pode simplesmente adicionar um loop infinito, usando um loop while , e sair do programa dependendo da entrada do player. Ex:

#!/usr/env bash

echo "Welcome to play Rock, Paper, Scissors"

choices=( 0 1 2 )

while [ "$PLAYER" != "Q" ]; do
    echo "Choose Rock(0), Paper(1), Scissors(2), or quit (Q)"
    read PLAYER

    #randomize the computer choice
    COMPUTER=${choices[$((RANDOM%3))]}

    if   [[ "$PLAYER" = "0" && "$COMPUTER" = "1" ]]
        then echo "i have paper , i win"
    elif [[ "$PLAYER" = "1" && "$COMPUTER" = "1" ]]
        then echo "i have paper , we draw"
    elif [[ "$PLAYER" = "2" && "$COMPUTER" = "1" ]]
        then echo "i have paper , you win, bye bye"
    elif [[ "$PLAYER" = "0" && "$COMPUTER" = "0" ]]
        then echo "i have rock , we draw"
    elif [[ "$PLAYER" = "1" && "$COMPUTER" = "0" ]]
        then echo "i have rock , you win, bye bye"
    elif [[ "$PLAYER" = "2" && "$COMPUTER" = "0" ]]
        then echo "i have rock , i win"
    elif [[ "$PLAYER" = "0" && "$COMPUTER" = "2" ]]
        then echo "i have scissors, you win, bye bye"
    elif [[ "$PLAYER" = "1" && "$COMPUTER" = "2" && "$count" = "0" ]]
        then echo "i have scissors, i win"
    elif [[ "$PLAYER" = "2" && "$COMPUTER" = "2" && "$count" = "0" ]]
        then echo "i have scissors, we draw "
    fi
done
    
por 30.04.2014 / 10:28