shell script (shopping games)

Requirements: A total of five stores, one by shopping, buy goods commodity information is printed, the amount of each purchase record. Walk around five shops give the total amount of output

Analysis: a cycle visiting five shops, small cycles asked whether the purchase of goods, buying every time, commodity prices accumulation.

#!/bin/bash
# Commodity price print function
info() {
        printf "        Price list
        1、hat 50¥
        2、shoes 200¥
        3、clothes 300¥
        4、trousers 100¥
        5、glove    30¥
"
}
# Commodity selection small cycles, and cumulative price.
shopping() {
        if [ $act == "y" ];then
                read -p "Please choose the goods.(1-5): " buy
                        case $buy in
                        1)
        # ++ array index used here, with the final price index corresponding value *
        # Is equivalent to let hatsum = $ [hatsum + 50]
        let hat[1]++
        Hatsum = $ [$ {has [1]} * 50]
        echo "You spent ${hatsum}¥ on the hat"
                        ;;
                        2)
        let shoes[2]++
        shoessum=$[${shoes[2]} * 200 ]
        echo "You spent ${shoessum}¥ on the shoes"
                        ;;
                        3)
        let clothes[3]++
        clothessum=$[${clothes[3]} * 300 ]
        echo "You spent ${clothessum}¥ on the clothes"
                        ;;
                        4)
        let tro[4]++
        trosum = $ [$ {believe [4]} * 100]
        echo "You spent ${trosum}¥ on the trosum"
                        ;;
                        5)
        let glove[5]++
        glovesum=$[${glove[5]} * 30 ]
        echo "You spent ${glovesum}¥ on the glove"
                        ;;
                        *)
        echo "please enter number 1-5......"
                        ;;
                        esac
        else
                break
        be
}
# 5 visiting a cycle store, if the implementation of small cycles, n to y, or others continue, skip this cycle. Where a determination is not strict, will not skip this cycle n.
for i in `seq 5`
do
        read -p "Pass the $i store,Go in and have a look?(y/n): " cho
        if [ "$cho" = "y" ];then
                while :
                do
                info
                read -p "Do you want to buy anything?(y/n): " act
                shopping
                done
        else
                continue
        be
done


Guess you like

Origin blog.51cto.com/13760226/2412448