Sheel script strategy--continue, break, echo commonly used escape characters, simple Taobao shopping script


One, continue and break

1. The difference and function of break and continue

  • Break and continue are used to control the loop structure, mainly to stop the loop
  • break
    • Sometimes we want to terminate the loop when a certain condition appears instead of waiting until the loop condition is false.
    • At this time we can use break to complete. break is used to completely end a loop, jump out of the loop body to execute the statement behind the loop
  • continue
  • continue and break are similar, the difference is that continue only terminates the current loop, and then executes the following loop, break completely terminates the loop
  • It can be understood that continue means skip the remaining statements in the current loop and execute the next loop

2. Break statement structure

mark

  • The break statement is used to terminate the execution of the entire loop
break
将用于以下break语句退出循环:

break n
这里n指定的第n个封闭的循环退出

3. Break application example

  • Try inserting a break from output 1~10
    mark
  • You can see that the value that could be output to 10 is only output to 5.
    mark

4. Continue statement structure

mark

  • The continue statement is similar to the break command, but it causes the current iteration of the loop to exit instead of the entire loop
  • This parameter is useful when an error has occurred, but you want to try to execute the next loop iteration
continue
和break语句一样,一个整数参数可以给continue命令跳过嵌套循环的命令

continue n
这里n指定第n个封闭循环 continue 

5.continue application example

mark
mark


Two, commonly used escape character-echo

1 Overview

echo -n     表示不换行输出
echo -e     输出转义符,将转义后的内容输出到屏幕上
  • Point, memorize it, or copy it!
Common escape characters Explanation
\b After escaping, it is equivalent to pressing the backspace key (backspace), but the premise is that there are characters after "b"; "\b" means deleting the previous character, and "\bb" means deleting the first two characters
\c Output without line break. When there are no characters after "\c", the function is equivalent to echo -n; but when there are still characters after "\c", the characters after "\c" will not be output
\n Line break, the characters to be output start on a new line from "\n"
\f Line break, but the beginning of the new line after the line break is connected to the end of the previous line
\ v Same as \f
\t After turning, it means inserting tab, that is, horizontal tab
\r The cursor moves to the beginning of the line, but does not wrap, which is equivalent to using the characters after "\r" to overwrite the characters of the same length before "\r"; but when there is no character after "\r", the one before "\r" Characters will not be overwritten
\ Means insert "" itself

2. Practice

[root@xcf ~]# echo -n "123456"
123456[root@xcf ~]# 
[root@xcf ~]# echo -e "123456"
123456

[root@xcf ~]# echo -e "123\b456"
12456
[root@xcf ~]# echo -e "123\b\b456"
1456
[root@xcf ~]# echo -e "123\b\b\b456"
456
[root@xcf ~]# echo -e "123456\b"
123456

[root@xcf ~]# echo -e "123\c456"
123[root@xcf ~]# echo -e "123456\c"
123456[root@xcf ~]# 
[root@xcf ~]# echo -e "\c123456"
[root@xcf ~]# 

[root@xcf ~]# echo -e "123\n456"
123
456
[root@xcf ~]# echo -e "123\f456"
123
   456
[root@xcf ~]# echo -e "123\v456"
123
   456

[root@xcf ~]# echo -e "123\t456"
123	456

[root@xcf ~]# echo -e "123\r456"
456
[root@xcf ~]# echo -e "123456\r"
123456

[root@xcf ~]# echo -e "123\\456"
123\456

Three, leave you to go home on the weekend big experiment: write a Taobao shopping script

1. Demand

  • You have entered a military supplies store, there are five kinds of goods for you to buy
  • Each time you purchase, you will be prompted whether to continue shopping. If you do not continue, the shopping cart will be settled.
  • Army coat 1,000 yuan, warm pants 500 yuan, combat boots 250 yuan, jungle camouflage hat 150 yuan, winter socks 50 yuan
  • After each purchase, the user will be prompted whether to continue shopping
  • If you do not continue shopping, the total shopping cart will be settled

2. Operate

  • The following case and if statements are used in the do···done loop in the for statement, nested
  • Learn and use, the methods are all thought of by people, not just this one
[root@localhost opt]# vim gouwu.sh

#!/bin/bash

sum=0

for ((;;))
do

echo "1:军大衣:1000元,2:保暖裤500元,3:作战靴250元,4:丛林迷彩帽150,5:冬袜50元"

read -p "请选择您想要购买的商品:" sg

case $sg in
1)
        echo "您已购买了一件大衣,花费1000元"
        let sum+=1000
;;
2)
        echo "您已购买了一条保暖裤,花费500元"
        let sum+=500
;;
3)
        echo "您已购买了一双作战靴,花费250元"
        let sum+=250
;;
4)
        echo "您已购买了一顶丛林迷彩帽,花费150元"
        let sum+=150
;;
5)
        echo "您已购买了一双冬袜,花费50元"
        let sum+=50
;;
*)
        echo "您本次消费共计$sum元,欢迎下次光临~"
        break
esac

read -p "您选择继续购物还是结算购物车?(yes/no)" pay

if [ $pay = yes ]
        then
                continue
elif [ $pay = no ]
        then
                echo "欢迎下次光临,您本次购物共计消费$sum元"
                break
else
        echo "请您正确输入"      
fi

done

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/111463304