shell 设置中午吃什么

每天吃什么都很纠结,用shell 生成一组随机数,把想到的吃的和每个数对应,运行脚本随机输出。

简单版本样例如下:

#!/bin/bash

choise=$(expr $RANDOM % 10)
echo $choise

if [ 0 -eq $choise ]; then
      echo "不吃了!"
elif [ 1  -eq $choise ]; then
      echo "米线"
elif [ 2 -eq $choise ]; then
      echo "KFC"
elif [ 3 -eq $choise ]; then
        echo "饭团"
elif [ 4  -eq $choise ]; then
      echo "麻辣烫"
elif [ 5 -eq $choise ]; then
      echo "大油条"
elif [ 6 -eq $choise ]; then
        echo "酸菜鱼"
elif [ 7  -eq $choise ]; then
      echo "永和豆浆"
elif [ 8 -eq $choise ]; then
      echo "西少爷"
elif [ 9 -eq $choise ]; then
        echo "subway"

fi

输出结果如下所示


[test]# bash lunch.sh
9
subway
[test]# bash lunch.sh
6
酸菜鱼
[test]# bash lunch.sh
0
不吃了!
[test]# bash lunch.sh
0
不吃了!
[test]# bash lunch.sh
2
KFC
[test]# bash lunch.sh
3
饭团
[test]# bash lunch.sh
0
不吃了!
[test]# bash lunch.sh
2
KFC
[test]# bash lunch.sh
8
西少爷
[test]# bash lunch.sh
9
subway
[test]# bash lunch.sh
5
大油条

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell中,可以使用if语句来判断时间。通过获取当前时间并与指定时间进行比较,可以根据不同的时间执行不同的任务。可以使用date命令获取当前时间,并使用自定义的时间格式。然后使用if语句来判断当前时间是否在指定的时间区间内。例如,你可以使用以下代码来判断当前时间是否在中午之前: ```shell time=`date +%H:%M:%S` if [[ "$time" > "00:00:01" ]] && [[ "$time" < "11:30:00" ]] then echo "当前时间还没到中午" else echo "已过中午,请休息" fi ``` 这个代码会获取当前时间,并与00:00:01和11:30:00进行比较,如果当前时间在这两个时间之间,则输出"当前时间还没到中午";否则输出"已过中午,请休息"。可以根据需要修改时间区间和输出内容。 另外,对于日期的比较,可以使用date命令将日期转换成时间戳格式,然后进行比较。例如,以下代码会将日期转换成时间戳格式,并比较两个日期的大小: ```shell date1="2008-4-09 12:00:00" date2="2008-4-10 15:00:00" t1=`date -d "$date1" +%s` t2=`date -d "$date2" +%s` if [ $t1 -gt $t2 ]; then echo "$date1 > $date2" elif [ $t1 -eq $t2 ]; then echo "$date1 == $date2" else echo "$date1 < $date2" fi ``` 这个代码会将date1和date2转换成时间戳格式,并比较它们的大小。根据比较结果输出不同的内容。可以根据需要修改日期和输出内容。 总结来说,Shell中的if语句可以用于判断时间。可以使用date命令获取当前时间并自定义格式,也可以将日期转换成时间戳格式进行比较。根据不同的条件执行不同的任务。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值