shell练习Day1

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿凯kk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值