Linux -- shell programming summary for loop, while loop, until loop

1. For loop: for loop will assign one parameter to var for loop format: for var in parameter do commands done Example: ...

1. For loop: for loop will assign one parameter to var
for loop format:

for var in parameter do commands done

Example:

for test in Nevada "New Hampshire" "New Mexico" "New York" do echo "Now going to $test" done

Read list from variable:

list="Alabama Alaska Arizona Arkansas Colorado" list=$list" Connecticut" for state in $list do echo "Have you ever visited $state?" done

Read value from command:

file="states" for state in $(cat $file) do echo "Visit beautiful $state" done

2. IFS is called internal field separator, which is used by shell script to split. By default, spaces, tabs and line breaks are used as separators
Change IFS field separator:

file="states" IFS=$'\n' for state in $(cat $file) do echo "Visit beautiful $state" done

If you want to specify multiple IFS characters, the format is as follows:

IFS=$'\n':;
IFS There are no restrictions on defining character parsing

3. C style for loop:

for (( var assigment ; condition ; iteration process ))

Example:

for (( i=1; i <= 10; i++ )) do echo "The next number is $i" done

Define multiple variables (only one judgment end condition is allowed):

for (( a=1, b=10; a <= 10; a++, b-- )) do echo "$a-$b" done

4. while loop executes as long as the judgment is true
Format:

while [ Judgement formula ] do commands done

Example:

var1=10 while [ $var1 -gt 0 ] do echo $var1 var1=$[ $var1 - 1 ] done

5. until loop: execute as long as the judgment is false
Format:

until [ Judgement formula ] do command done

Example:

var1=100 until [ $var1 -eq 0 ] do echo $var1 var1=$[ $var1 - 25 ] done

6. Nested loop:
Example:

for (( a = 1; a <= 3; a++ )) do echo "Starting loop $a:" for (( b = 1; b <= 3; b++ )) do echo "Inside loop $b:" done done

Loop processing / etc/passwd:

IFS.OLD=$IFS IFS=$'\n' for entry in $(cat /etc/passwd) do echo "Value is $entry" IFS=: for value in $entry do echo " $value" done done

Another command is provided: cut, - d: specify separator, - f: specify column, - b for byte, - c for character, and the default separator is tab
For example: cut - D: - F 2 / etc / passwd cut - D: - B 1-5 / etc / passwd
7. Exit loop break command: used to jump out of loop
Example:

for var1 in 1 2 3 4 5 6 7 8 9 10 do if [ $var1 -eq 5 ] then break fi echo "Iteeration number: $var1" done echo "The for loop is completed"

The break can be followed by a number to represent the level of the stop loop
Example:

for (( a=1; a<4; a++ )) do echo "Outer loop $a" for (( b=1; b<100; b++ )) do if [ $b -gt 4 ] then break 2 fi echo " Inner loop: $b" done done

8. Pause a cycle continue
Example:

for (( var1=1; var1<15; var1++ )) do if [ $var1 -gt 5 ] && [ $var1 -lt 10 ] then continue fi echo "Iteration number: $var1" done

continue followed by a number is the same as break
Example:

for (( a = 1; a <= 5; a++ )) do echo "Iteration $a:" for (( b = 1; b < 3; b++ )) do if [ $a -gt 2 ] && [ $a -lt 4 ] then continue 2 fi var3=$[ $a * $b ] echo "The reault of $a * $b os $var3" done done

9. Handle the output of the loop. You can use the redirection symbol after the done to facilitate reading and management
Example:

for (( a = 1; a < 10; a++ )) do echo "The number is $a" done > test.txt echo "The command is finished."

10. Example 1: find the executable file. When executing a program, the shell will go to the PATH environment variable to find it
Example:

IFS=: for folder in $PATH do echo "$folder:" for file in $folder/* do if [ -x $file ] then echo " $file" fi done done
it-wtyy Published 6 original articles, won praise 1, visited 150 Private letter follow

4 February 2020, 01:27 | Views: 2832

Add new comment

For adding a comment, please log in
or create account

0 comments