天天看點

Linux 金字塔 的shell指令,寫一個猜數字腳本,當使用者輸入的數字和預設數字(随機生成一個0-100的數字)一樣時,直接退出并顯示猜了多少次,否則讓使用者一直輸入,并提示數字的大小...

#!/bin/bash

#定義一個變量$num,取出1到100的随機值

num=`echo $((RANDOM%100+1))`

i=1

#輸入一個數值($num1)

read -p "please enter integer:" num1

#while循環

while true;do

#使用$num1和$num比較

if [ $num1 -gt $num ];then

echo "The number you entered is too big."

read -p "please enter integer:" num1

elif [ $num1 -lt $num ];then

echo "The number you entered is too small."

#當$num1不等于$num的時候,提示使用者重新輸入$num1

read -p "please enter integer:" num1

else

#當輸入的$num1和$num相同時,輸出"You WIN"

echo "You WIN"

break

fi

#依次遞增,當$num1和$num不相等時繼續循環

let i++

done

#輸出你總共輸出了多少($i)次

echo "You tried $i times"

Linux 金字塔 的shell指令,寫一個猜數字腳本,當使用者輸入的數字和預設數字(随機生成一個0-100的數字)一樣時,直接退出并顯示猜了多少次,否則讓使用者一直輸入,并提示數字的大小...