您好,登錄后才能下訂單哦!
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
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。