1.猜数字
#!/bin/bash
#-eq //equals等于
#-ne //no equals不等于
#-gt //greater than 大于
#-lt //less than小于
#-ge //greater equals大于等于
#-le //less equals小于等于
#注:
#1 在shell中进行比较时,结果为0代表真,为1代表假。
#2 -eq,-ne等比较符只能用于数字比较,有字符也会先转换成数字然后进行比较。
echo "Welcome to NumberGuessing game!"
num=$[RANDOM%100+1]
tell_num=$(((( $num/10 ))*10))
cycle=0
while :
do
read -p "Please input the number you guess:" GuessNumb
if [ $GuessNumb -eq $num ];then
echo "Bingo! You win!"
exit
elif [ $GuessNumb -gt $num ];then
echo "Wrong numb! Your numb is greater than real numb."
else
echo "Wrong numb! Your numb is less than real numb."
fi
cycle=$[ $cycle+1 ]
if [ $cycle -eq 3 ];then
echo "You have tried 3 times, the real numb is closed to $tell_num"
elif [ $cycle -gt 6 ];then
echo "You have tried more than 6 times, the real numb is $num"
echo "You lost!"
exit
else
continue
fi
done
2.石头剪刀布
#!/bin/bash
echo "Welcome to Rock Scissor Paper game!"
echo "Please input like the following examples."
echo "1.Rock"
echo "2.Scissor"
echo "3.Paper"
array=(Rock Scissor Paper)
num=$(expr $RANDOM % 2 + 1)
#computer=${array[$num]}
read -p "Please input you number(1-3):" number
case $num in
1)
if [ $number -eq 1 ];then
echo "Play even."
elif [ $number -eq 2 ];then
echo "You lost."
elif [ $number -eq 3 ];then
echo "You win."
else
echo "Please input correct number(1-3)"
fi;;
2)
if [ $number -eq 1 ];then
echo "You win."
elif [ $number -eq 2 ];then
echo "Play even."
elif [ $number -eq 3 ];then
echo "You lost."
else
echo "Please input correct number(1-3)"
fi;;
3)
if [ $number -eq 1 ];then
echo "You lost."
elif [ $number -eq 2 ];then
echo "You win."
elif [ $number -eq 3];then
echo "Play even."
else
echo "Please input correct number(1-3)"
fi;;
*)
echo "error number"
esac
3.三个数,从小到大排序
#!/bin/bash
read -p "Please input your first num:" num1
read -p "Please input your second num:" num2
read -p "Please input your third num:" num3
temp=0
if [ $num1 -gt $num2 ];then
temp=$num1
num1=$num2
num2=$temp
fi
if [ $num1 -gt $num3 ];then
temp=$num1
num1=$num3
num3=$temp
fi
if [ $num2 -gt $num3 ];then
temp=$num2
num2=$num3
num3=$temp
fi
echo "$num1,$num2,$num3"
4.1-100求和
#!/bin/bash
#1-100求和
sum=0
for i in `seq 100`
do
sum=$[sum+i]
done
echo $sum