Linux编程:使用if语句、select语句 、case三种语句,输入one时,显示“Today is Monday”输入two时,显示“Today is Tuesday”输入three时

今天下雨啦,心情是无聊且郁闷啊!
来写写今天Linux老师布置的作业吧~~~
在这里插入图片描述
题目如下:
在这里插入图片描述
其实,这道题怎么说呢?
看起来很简单,做起来也很简单。。。。。。
思路:
题目要求用if,select,case三种方法,
那我就先用select语句判断要用的方法,
然后用case语句选择对应的方法的功能。

废话不多说,上代码。
代码如下:

#!/bin/sh
echo "Please enter the method you want to use(1,2,3):"
#先用select语句判断要用的方法
select i in "if" "select" "case"
do
        echo "you chose the ($i) way:"
        break
done
#用case语句选择对应的方法的功能
case $i in
		#if方法
        "if")echo "The method of if:"
        echo "please enter the number(one,two,three):"
        #用while循环来嵌套,即使输入错了也会提示重新输入。
        while [ 0 ]
        do
                read num
                if [ $num = "one" ];then
                        echo "Today is Monday"
                        break
                elif [ $num = "two" ];then
                        echo "Today is Tuesday"
                        break
                elif [ $num = "three" ];then
                        echo "Today is Wednesday"
                        break
                else
                        echo -e "enter error! please enter again:\c"
                fi
        done;;

		#select方法
        "select")echo "Please enter the method you want to use(1,2,3):"
                select num in "Monday" "Tuesday" "Wednesday"
                do
                        echo "Today is $num"
                        break
                done;;

		#case方法
        "case")echo "The method of case:"
                echo "please enter the number(one,two,three):"
                read num
                case $num in
                        "one")echo "Today is Monday";;
                        "two")echo "Today is Tuesday";;
                        "three")echo "Today is Wednesday";;
                        *)echo "enter error!"
                        esac;;

        *)echo "enter error!"
esac

贴一下其中的运行截图:
在这里插入图片描述

在这里提醒一下兄弟们,
变量引用要加$ , 判 断 条 件 要 加 空 格 如 : 错 误 : [ ,判断条件要加空格 如: 错误:[ ,[num=“one”]
正确:[空格$num空格=空格"one"空格]
!!!
憨憨的我踩过的坑!兄弟们,我帮你们填平了,可别踩了!
就这样,我要走了,beybey,宝。
在这里插入图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

加辣椒了吗?

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

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

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

打赏作者

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

抵扣说明:

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

余额充值