Shell编程之流程控制——案例使用
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,Shell编程之流程控制--案例使用shell 编程中所包含的流程控制语句有if case for while下面结合上述控制流程语句格式写出一个简单案例,将其互相嵌套使用,适合新手学习,shell熟
千家信息网最后更新 2026年11月03日Shell编程之流程控制——案例使用
Shell编程之流程控制--案例使用
shell 编程中所包含的流程控制语句有if case for while
下面结合上述控制流程语句格式写出一个简单案例,将其互相嵌套使用,适合新手学习,shell熟练者请自动忽略。
#!/bin/bashecho "欢迎来到我的超市!"echo "正在加载页面,加载中,请稍后3s......"sleep 3echo -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) :" numlet price1=128let price2=118let price3=228let price4=12let price5=45let price6=2let sum=0let sum1=0let sum2=0let count1=0let count2=0let count3=0let count4=0let count5=0let count6=0echo -e "shoplist price" > shoplist.txtwhile 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 fidoneif [ $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
商品
文具
衣服
控制
文具店
输入
案例
编程
买点
夹克
夹克衫
序号
正在
流程
清单
牛仔
牛仔裤
语句
货物
熟练
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
广东万易互联网科技有限公司惠州分公司
互联网科技公司经营范围
河南真二互联网科技有限公司
上海互联网科技有限公司
互联网科技的发展
齐家互联网科技
互联网科技咨询
成都仁贤领冠互联网科技有限公司
海南科技产业互联网平台
互联网 科技馆
承德科技产业互联网发展
中国航天科技大学国际工业互联网
允成互联网科技有限公司融资
互联网科技快讯热点
从那以后互联网科技有限公司
版纳互联网科技选哪家
长沙易易互联网科技有限公司
互联网科技大佬云集
重庆科技产业互联网有哪些
如何强化互联网金融科技
云信互联网科技有限公司
互联网科技活动banner
互联网金融监管科技数据
广州宏创互联网科技
赫美联合互联网科技
互联网科技创新企业排名
深圳市运趣互联网科技有限公司
互联网科技有哪些公司
东营紫格互联网科技
易企秀互联网科技是做什么的