We do a little game: rock-paper-scissors
First we look at the provisions of the rules:
1. The user inputs a value representative of stone scissors cloth, compared with the computer-generated random value
2. Only the user can exit the game won
. 1 ! # / Bin / the bash 2 . 3 arryas = (rock paper scissors) . 4 the while to true . 5 do . 6 NUM = $ (($% the RANDOM . 3 )) . 7 . 8 . 9 Read -p " Enter Number: 1. 0. rock scissors 2 cloth " username 10 . 11 echo -e " you are out of $ {arryas [$ username]} VS computer out arryas {$ [$ NUM]} \ n- " 12 is 13 is dd = $ (($ username- $ NUM)) 14 IF [$ dd -eq . 1 -o $ dd -eq - 2 ] 15 the then 16 echo " you win " . 17 Exit 18 is elif [$ dd -eq 0 ] . 19 the then 20 is echo " tie " 21 is the else 22 is echo " you lose " 23 is Fi 24 DONE
test:
. [the root @ Logan Day4] # the bash SJB SH input number: 0 . scissors 1 . stones 2 . cloth 1 you out stones VS computer cloths you lose input number: 0 . scissors 1 . stones 2 . cloth 2 you a cloth VS PC out of the cloth draw input number: 0 . scissors 1 . stone 2 . fabric 1 you out of stone VS PC out of the stone draw input number: 0 . scissors 1 . stone 2 . cloth 0 you out scissors VS computer cloths you win [root @ logan day4] #
carry out