Shell programming-echo parameter expansion for, while, until three loop statements plus break to jump out of the loop continue to stop the loop IFS field separator-illustration

Insert picture description here

Echo parameters

  • echo -n # means no newline output
    Insert picture description here

  • echo -e output escape characters, output the escaped content to the screen (need to be used with double quotation marks)

  • Commonly used escape characters are:\b \c \n \f \v \t \r \\

    • \b : After escaping, it is equivalent to pressing the backspace key (backspace), but only if there are characters after "\b": "\b" means deleting the previous character, and "\b\b" means deleting the first two characters
      Insert picture description here

    • \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
      Insert picture description here

    • \n : New line, the characters to be output start on a new line from "\n". In Linux, \n has the meaning of carriage return
      Insert picture description here

    • \f : Newline, but the beginning of the new line after the newline is connected to the end of the previous line
      Insert picture description here

    • \v : same as \f

    • \t : After escaping, it means inserting a tab, that is, a horizontal tab
      Insert picture description here

    • \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", "\r" "The preceding character will not be overwritten
      Insert picture description here

    • \\ : means to insert "\" itself
      Insert picture description here

loop statement

for loop statement

  • Read different variable values ​​to execute the same set of commands one by one (collectively referred to as traversal operation)
  • format
for 变量名 in 取值列表
do
  命令序列
done

Insert picture description here

  • The simplest for loop example

    • The first way of expression: i in {1…10} —— i in {1…10…2} 2 means adding 2 after each loop in an iterative manner
      Insert picture description here
    • The second way of expression: i in $(seq 1 10) —— i in $(seq 1 2 10) has the same meaning as above
      Insert picture description here
    • The third way of expression: (i=1; i<10; i++) —— (i=1;i<=10;i+=2) Same meaning as above
      Insert picture description here
  • Live learning and application example 1
    Add users in batches: the user names are stored in the users.txt file, each line, the initial password is set to 123456
    Insert picture description here
    Insert picture description here

  • Live learning example 2
    Check the status of the host based on the IP address. The IP address is stored in the ipadds.txt file, one per line. Use the ping command to check the connectivity of each host
    Insert picture description here
    Insert picture description here

while loop statement

  • Test a certain condition repeatedly, and execute it repeatedly as long as the condition is true
  • format
while 条件测试操作
do
  命令序列
done

Insert picture description here

  • While simple operation (the insertion position of the iteration in the output content has a certain relationship)
    Insert picture description here
  • Some examples of the application of while statement
    1. Add users in batches: The user name must start with stu and be numbered in numerical order. There are 20 in total, namely stu1...stu20, and the password is set to 123456
    Insert picture description here
  • Difficulty upgrade
    2. Guess the commodity price game: get a random number through the variable RANDOM, prompt the user to guess and record the number of times, and exit the loop after the guess is successful
  • RANDOM can randomly get random values ​​from 0-32767
  • If we want to generate numbers in the range of 0-25:$(($RANDOM%26))
  • If you want to get a number in the range of 1–68: $(($RANDOM%68+1 ))
  • If you want to get a number in the range of 6-87: $(($RANDOM%82+6 ))
    Insert picture description here
    Insert picture description here

until loop statement

  • Test a certain condition repeatedly, and execute it repeatedly as long as the condition is not established
  • Equivalent to while in turn with while, until seems a bit tasteless
until 条件测试操作
do 
  命令序列
done

Insert picture description here

  • until simple operation
    Insert picture description here
  • Some examples of until statement
    1. Calculate the sum value from 1 to 50, and calculate by loop accumulation
    Insert picture description here

break out of the loop

  • break : Jump out of the loop that contains break
  • Simple double loop
    Insert picture description here
    Insert picture description here
  • Jump out of the single-layer loop in the inner loop
    Insert picture description here
    Insert picture description here
  • break is sequential

Insert picture description here

  • break 2:It means that the number after jumping out of the 2nd layer of loop is how many layers are out of the loop

continue to abort a single loop

  • continue : Abort a command in a loop, but will not completely stop the entire command
    Insert picture description here
    Insert picture description here
  • It is not recommended to use continue in a while loop
    Insert picture description here
    #Continue is used in while. At this time, the position of the iterative code is very important and will affect the result of the entire code.

IFS field separator

  • IFS field separator
    contains spaces , tabs, and newlines by default\t\n

If a piece of content edited in users.txt has spaces
Insert picture description here
Insert picture description here

  • IFS=$' \t\n'
    Insert picture description here
  • You can redefine the variable of IFS
    IFS=$'\n'# to only recognize the newline character
  • What should I do if the space cannot be recognized after IFS is modified and other operations are performed
IFS=$'\n'    #进行重新定义变量IFS只识别换行符
OLDIFS=$' \t\n'   #重新定义之前的内容
.......
IFS=$OLDIFS   #把定义的变量重新定义给IFS

Guess you like

Origin blog.csdn.net/weixin_53496398/article/details/114446196