shell while循环简单猜数字游戏

 

#! /bin/bash
int=1
while(("$int"<=5))
do
	echo "$int"
	let "int++"
done

 

#! /bin/bash
#	猜数字游戏
echo "请输入1~10整数"
read num
count=0
#       此处使用两个中括号[[ ]]和一个中括号[ ]都可以,但是不能使用(())或 ()
while [[ $num -ne 6 ]]
do
	let "count++"
	if [ $num -lt 1 -o $num -gt 10 ]
	then
		echo "请输入1-10的整数"
		read num
	elif [ $num -lt 6 ]
	then
		echo "too small,try again"
		read num
	elif [ $num -gt 6 ]
	then
		echo "too big,try again"
		read num
	else
		exit 0
	fi
done
echo "you are right ,yes the right number is 6.you guess $count times!"

 

 

你可能感兴趣的:(shell脚本编程,shell,while,循环,猜数字,游戏)