Flow Control of Shell programming-- case use
Flow Control of Shell programming-- case use
The flow control statements included in shell programming are if case for while
The following combined with the above control flow statement format to write a simple case, will be nested with each other to use, suitable for novice learning, shell proficient please automatically ignore.
#! / bin/bashecho "Welcome to my supermarket!" echo "is loading the page. Please wait 3s." sleep 3echo-e "\ 033 [1it36m #\ 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" Please enter your choice (1 or 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 "about to leave!" Exit 0 else echo "has the following two stores!" Echo-e "1) clothes\ t 2) stationery" read-p "please input your choice {1 or 2}:" choice case $choice in 1) echo-e "\ 033 [5it36m Welcome to the clothing store!\ 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" what do you want to buy? Please enter the serial number {1 or 2 or 3}, and the other keys will leave: "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 [5ter36m Welcome to the stationery store!\ 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" what do you want to buy? Please enter the serial number {1 or 2 or 3}, and the other keys will leave: "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 "your purchase list is:" cat shoplist.txt echo "in which you bought $count1 shoes, $count2 pair of jeans, and $count3 jackets at the clothing store! Spend a total of $sum1 "echo" in this store, of which you bought $count4 pens, $count5 books, and $count6 ruler at the stationery store! The total cost in this store is ¥$sum2 "echo". The total price of the goods is being calculated. Please wait for 3 seconds. " Sleep 3 echo "your cost total is $sum" elif [$sum-gt 500] then echo-e "your purchase list is:\ r" cat shoplist.txt echo "in which you bought $count1 shoes, $count2 pairs of jeans and $count3 jackets at the clothing store! Spend a total of $sum1 "echo" in this store, of which you bought $count4 pens, $count5 books, and $count6 ruler at the stationery store! You spent $sum2 "echo" at the store. You spent $sum, so get ready to go back and kneel on the keyboard. Else echo "you came to shop and didn't buy anything." fi