Detailed shell script (three)-for loop of loop statement

1. The echo command-output a string or extract the value of a Shell variable

1. Format

Insert picture description here

2. Common parameters

Common parameters Description
-n Indicates that the output does not wrap
-E Backslash escaping is prohibited, which is the opposite of the -e parameter
-e Output escape characters, and output the escaped content to the screen \b It is equivalent to the backspace key, but the premise is that there is a character after "\b".
"\b" means to delete the previous character.
"\b\b" means to delete the first two characters
\c Do not wrap the output. When there is no character behind, it is equivalent to echo -n
. When there is character after "\c", it will not be output
\n Line break, but the new line after the line break starts on a new line at "\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 escaping, it means to insert 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 numbers after "\r" to overwrite the characters of the same length before
\\ Means to insert "\" itself

3. Example

Two, for loop statement

1. For loop structure

  • Read different variable values ​​to execute the same group of commands one by one

Insert picture description here

2. Examples

①, example 1

  • Calculate the sum of all integers from 1 to 100

Insert picture description here

②, example 2

  • Filter out the odd and even numbers of all integers from 1 to 100

Insert picture description here
Insert picture description here

③, sample question 3

  • Prompt the user to enter an integer less than 100, and calculate the sum of all integers from 1 to this number

Insert picture description here

Insert picture description here

④. Example 4

  • Find the even and odd sums of all integers from 1 to 100

Insert picture description here

Insert picture description here

⑤, Example 5

  • Detect whether the host in the specified range is communicating, and output the communicating host ip to the file host_ip 4

Insert picture description here

Insert picture description here

⑥. Example 6

  • Output all executable files in the /dev directory

Insert picture description here

Insert picture description here

⑦、Example 7

  • Execute the script to enter the user name, if the user exists, the output prompts that the user already exists; if the user does not exist, prompt the user to enter the password, create the user and set up its password

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/Lucien010230/article/details/114379211