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

今天下雨啦,心情是无聊且郁闷啊!
来写写今天Linux老师布置的作业吧~~~
Linux编程:使用if语句、select语句 、case三种语句,输入one时,显示“Today is Monday”输入two时,显示“Today is Tuesday”输入three时_第1张图片
题目如下:
Linux编程:使用if语句、select语句 、case三种语句,输入one时,显示“Today is Monday”输入two时,显示“Today is Tuesday”输入three时_第2张图片
其实,这道题怎么说呢?
看起来很简单,做起来也很简单。。。。。。
思路:
题目要求用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

贴一下其中的运行截图:
Linux编程:使用if语句、select语句 、case三种语句,输入one时,显示“Today is Monday”输入two时,显示“Today is Tuesday”输入three时_第3张图片

在这里提醒一下兄弟们,
变量引用要加$ , 判 断 条 件 要 加 空 格 如 : 错 误 : [ ,判断条件要加空格 如: 错误:[ ,[num=“one”]
正确:[空格$num空格=空格"one"空格]
!!!
憨憨的我踩过的坑!兄弟们,我帮你们填平了,可别踩了!
就这样,我要走了,beybey,宝。
Linux编程:使用if语句、select语句 、case三种语句,输入one时,显示“Today is Monday”输入two时,显示“Today is Tuesday”输入three时_第4张图片

你可能感兴趣的:(shell,linux)