Catalog title
Echo parameters
-
echo -n # means no newline output
-
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
-
\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 : New line, the characters to be output start on a new line from "\n". In Linux, \n has the meaning of carriage return
-
\f : Newline, but the beginning of the new line after the newline is connected to the end of the previous line
-
\v : same as \f
-
\t : After escaping, it means inserting a tab, that is, a 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", "\r" "The preceding character will not be overwritten
-
\\ : means to insert "\" itself
-
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
-
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
- The second way of expression: i in $(seq 1 10) —— i in $(seq 1 2 10) has the same meaning as above
- The third way of expression: (i=1; i<10; i++) —— (i=1;i<=10;i+=2) Same meaning as above
- 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
-
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
-
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
while loop statement
- Test a certain condition repeatedly, and execute it repeatedly as long as the condition is true
- format
while 条件测试操作
do
命令序列
done
- While simple operation (the insertion position of the iteration in the output content has a certain relationship)
- 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
- 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 ))
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
- until simple operation
- Some examples of until statement
1. Calculate the sum value from 1 to 50, and calculate by loop accumulation
break out of the loop
- break : Jump out of the loop that contains break
- Simple double loop
- Jump out of the single-layer loop in the inner loop
- break is sequential
- 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
- It is not recommended to use continue in a while loop
#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
-
IFS=$' \t\n'
- 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