Shell编程之流程控制——案例使用
admin
2023-02-26 02:40:58
0

Shell编程之流程控制——案例使用

shell 编程中所包含的流程控制语句有if case for while

下面结合上述控制流程语句格式写出一个简单案例,将其互相嵌套使用,适合新手学习,shell熟练者请自动忽略。

#!/bin/bash
echo "欢迎来到我的超市!"
echo  "正在加载页面,加载中,请稍后3s......"
sleep 3
echo -e "\033[1;36m ############################################ \033[0m"
echo -e "\033[1;36m -----------------follow choice-------------- \033[0m"
echo -e "1)shopping \t 2)exit  "
echo -e "\033[1;36m ############################################ \033[0m"
read -p "请输入你的选择(1或者2) :" num
let price1=128
let price2=118
let price3=228
let price4=12
let price5=45
let price6=2
let sum=0
let sum1=0
let sum2=0
let count1=0
let count2=0
let count3=0
let count4=0
let count5=0
let count6=0
echo -e "shoplist  price" > shoplist.txt
while true                                  
do
    if [ $num -ne 1 ]
    then
        echo "即将离开!"
        exit 0
    else
        echo "有以下两个商店!"
        echo -e "1)衣服 \t 2)文具 "
        read -p "please input your choice {1 or 2}:" choice
        case $choice in 
        1)
            echo -e "\033[5;36m 欢迎来到衣服店! \033[0m"
            while true
            do 
                echo -e "\033[1;35m-----------goods list------------------ \033[0m"
                echo -e "1)shoes ¥128 \t2)jeans ¥118 \t3)jacket ¥228"
                read -p "你想买点啥?请输入商品序号 {1 or 2 or 3},其他键则离开:" key
                case $key in 
                1)
                    echo "your choice is $key"
                    echo -e "shoes\t ¥128" >>  shoplist.txt
                    let sum+=$price1
                    let sum1+=$price1
                    let count1+=1
                    ;;
                2)
                    echo "your choice is $key"
                    echo -e "jeans\t ¥118" >>  shoplist.txt
                    let sum+=$price2
                    let sum1+=$price2
                    let count2+=1
                    ;;
                3)
                    echo "your choice is $key"
                    echo -e "jacket\t ¥228" >>  shoplist.txt
                    let sum+=$price3
                    let sum1+=$price3
                    let count3+=1   
                    ;;
                *)  
                    echo "you choose leave,welcome to come again!"
                    echo -e "\r"
                    break
                esac
            done
            ;;
        2)
            echo -e "\033[5;36m 欢迎来到文具店! \033[0m"
            while true
            do 
                echo -e "\033[1;34m-----------goods list------------------ \033[0m"
                echo -e "1)pen ¥12 \t 2)book ¥45 \t 3)ruler ¥2"
                read -p "你想买点啥?请输入商品序号 {1 or 2 or 3},其他键则离开:" key
                case $key in 
                1)
                    echo "your choice is $key"
                    echo -e "pen\t ¥12" >>  shoplist.txt
                    let sum+=$price4
                    let sum2+=$price4
                    let count4+=1
                    ;;
                2)
                    echo "your choice is $key"
                    echo -e "book\t ¥45" >>  shoplist.txt
                    let sum+=$price5
                    let sum2+=$price5
                    let count5+=1
                    ;;
                3)
                    echo "your choice is $key"
                    echo -e "ruler\t ¥2" >>  shoplist.txt
                    let sum+=$price6
                    let sum2+=$price6
                    let count6+=1   
                    ;;
                *)  
                    echo "you choose leave,welcome to come anain!"
                    echo -e "\r"
                    break
                esac
            done
            ;;
        *)
            echo "you may come to other market!"
            break
        esac
    fi
done
if [ $sum -ne 0 ]&&[ $sum -le 500 ]
then 
    echo "您购买的货物清单是:"
    cat shoplist.txt
    echo "其中你在衣服店买了$count1双鞋,买了$count2条牛仔裤,买了$count3件夹克衫!在该店一共花费¥$sum1"
    echo "其中你在文具店买了$count4支笔,买了$count5本书籍,买了$count6把尺!在该店一共花费¥$sum2"
    echo "正在计算商品总价,请稍等3s......"
    sleep 3
    echo "your cost total is  ¥ $sum"
elif [ $sum -gt 500 ]
then
    echo -e "您购买的货物清单是:\r"
    cat shoplist.txt
    echo "其中你在衣服店买了$count1双鞋,买了$count2条牛仔裤,买了$count3件夹克衫!在该店一共花费¥$sum1"
    echo "其中你在文具店买了$count4支笔,买了$count5本书籍,买了$count6把尺!在该店一共花费¥$sum2"
    echo "你花了¥ $sum,准备回去跪键盘吧!"
else
    echo "你来逛了一下,并没有买任何商品"
fi

相关内容

热门资讯

日本皇室恐上演养子夺嫡,麻生太... 澎湃新闻记者 陈沁涵日本政府7月24日正式公布修订后的《皇室典范》,核心在于确保足够的皇族人数。这是...
菲尔兹奖美国得主走红:会说中文... 当地时间7月23日,在美国费城举行的2026年国际数学家大会开幕式上,中国籍数学家王虹、邓煜获得菲尔...
中纪委连打三“虎” 今天(25日),中央纪委国家监委网站连发三份通报。肖杰被开除党籍经中共中央批准,中央纪委国家监委对海...
扎根硬核场景,璇玑动力以全栈自... 2026 世界人工智能大会落地三地四馆,具身智能成为全场核心主线,超 200 家企业同台竞技。展区内...
靠谱大容量TF卡推荐:三星T7... 三星原厂 · 全能微存储新基准 当平板扩容需求激增、Switch游戏掌机需加载多款3A游戏、航拍无人...
买不起大疆、影石的年轻人,排队... 文 | 表外表里 ,作者 | 陈梓洁,编辑 | 杨静、曹宾玲 “一台机器,旺季月入1500。” 去...
美国众议员当街打掉拍摄人员的手... 近日,美国KCCI电视台爆出的一段视频显示,美国众议员扎克·纳恩在街道上与跟踪拍摄人员发生口角,并动...
今天上凯道人数成关键!媒体人示... 海峡导报综合报道 台湾中联油脂致癌油风暴继续延烧,国民党今天(25日)在凯道举办“我是人、我反毒台”...
谷歌公布1500万次AI交互,... 导读:谷歌分析1500万次去标识化AI交互后发现,在一个典型职业中,AI大约可参与21%的任务,但完...
空调水管不出水不制冷 原因可能是空调缺氟或是氟利昂泄漏,可以修补泄漏点,添加适量的氟;原因可能是空调的功率不够,可以更换与...