YouTip LogoYouTip

Linux Shell Process Control

Unlike languages such as Java or PHP, the flow control in sh cannot be empty. For example (the following is PHP flow control syntax): ## Example ` and ` b )); then ...fi The following example checks if two variables are equal: ## Example a=10 b=20 if[$a == $b] then echo"a equals b" elif[$a-gt$b] then echo"a is greater than b" elif[$a-lt$b] then echo"a is less than b" else echo"no matching condition" fi Output: a is less than b Using `((...))` as the test statement: ## Example a=10 b=20 if(($a == $b)) then echo"a equals b" elif(($a>$b)) then echo"a is greater than b" elif(($a<$b)) then echo"a is less than b" else echo"no matching condition" fi Output: a is less than b The if else statement is often used with the test command, as shown below: ## Example num1=$[2*3] num2=$[1+5] if test $-eq $ then echo'The two numbers are equal!' else echo'The two numbers are not equal!' fi Output: The two numbers are equal! * * * ## for loop Similar to other programming languages, Shell supports for loops. The general format of a for loop is: for var in item1 item2 ... itemN do command1 command2 ... commandN done Written in one line: for var in item1 item2 ... itemN; do command1; command2… done; When the variable value is in the list, the for loop executes all commands once, using the variable name to get the current value in the list. Commands can be any valid shell commands and statements. The `in` list can contain substitutions, strings, and filenames. The `in` list is optional. If omitted, the for loop uses the positional parameters of the command line. For example, to sequentially output the numbers in the current list: ## Example for loop in 1 2 3 4 5 do echo"The value is: $loop" done Output: The value is: 1The value is: 2The value is: 3The value is: 4The value is: 5 To sequentially output the characters in a string: #!/bin/bashfor str in This is a stringdo echo $str done Output: Thisis a string * * * ## while statement The while loop is used to execute a series of commands repeatedly, and also to read data from an input file. Its syntax format is: while condition do command done Here is a basic while loop. The test condition is: if `int` is less than or equal to 5, the condition returns true. `int` starts at 1, and with each loop iteration, `int` is incremented by 1. Running the above script returns numbers 1 to 5, then terminates. ## Example #!/bin/bash int=1 while(($int<=5)) do echo$int let"int++" done Running the script, the output is: 12345 The above example uses the Bash `let` command, which is used to execute one or more expressions. In variable calculations, you do not need to add `$` to represent the variable. For details, see: (#). The while loop can be used to read keyboard information. In the following example, the input information is set to the variable `FILM`, and the loop ends by pressing . ## Example echo'Press to exit' echo-n'Enter your favorite website name: ' while read FILM do echo"Yes! $FILM is a good website" done Running the script, the output is similar to: Press to exitEnter your favorite website name:Yes! is a good website ### Infinite loop Infinite loop syntax format: while :do command done Or while truedo command done Or for (( ; ; )) * * * ## until loop The until loop executes a series of commands until the condition becomes true. The until loop is the opposite of the while loop in terms of handling. Generally, the while loop is preferred over the until loop, but in some casesβ€”though very rarelyβ€”the until loop is more useful. until syntax format: until condition do command done The condition is usually a conditional expression. If the return value is false, the statements inside the loop body continue to execute; otherwise, the loop exits. In the following example, we use the until command to output numbers from 0 to 9: ## Example #!/bin/bash a=0 until[!$a-lt 10] do echo$a a=`expr$a + 1` done Running result: The output is: 0123456789 * * * ## case ... esac **case ... esac** is a multi-choice statement, similar to the switch ... case statement in other languages. It is a multi-branch selection structure. Each case branch starts with a right parenthesis and uses two semicolons `;;` to represent break, meaning the execution ends, exiting the entire case ... esac statement. `esac` (which is `case` spelled backwards) serves as the end marker. You can use the case statement to match a value against a pattern. If the match is successful, the corresponding commands are executed. The syntax format for **case ... esac** is as follows: case Value in Mode 1) command1 command2 ... commandN ;;Mode 2) command1 command2 ... commandN ;;esac The case works as shown above. After the value, the word **in** must follow. Each pattern must end with a right parenthesis. The value can be a variable or a constant. Once the value is found to match a pattern, all commands between that pattern and the next `;;` are executed. The value will be tested against each pattern. Once a pattern matches, after executing the corresponding commands, no further patterns are tested. If no pattern matches, the asterisk `*` is used to capture the value, and the subsequent commands are executed. The following script prompts for input between 1 and 4 and matches each pattern: ## Example echo'Enter a number between 1 and 4:' echo'You entered:' read aNum case$aNum in 1)echo'You chose 1' ;; 2)echo'You chose 2' ;; 3)echo'You chose 3' ;; 4)echo'You chose 4' ;; *)echo'You did not enter a number between 1 and 4' ;; esac Entering different content yields different results, for example: Enter a number between 1 and 4:You entered:3You chose 3 The following script matches strings: ## Example #!/bin/sh site="tutorial" case"$site"in "tutorial")echo"" ;; "google")echo"Google Search" ;; "taobao")echo"Taobao" ;; esac The output is: * * * ## Breaking out of loops During loop execution, sometimes you need to forcibly exit the loop before reaching the loop termination condition. Shell uses two commands to achieve this: **break** and **continue**. ### break command The break command allows you to exit all loops (terminate the execution of all subsequent loops). In the following example, the script enters an infinite loop until the user enters a number greater than 5. To exit this loop and return to the shell prompt, you need to use the break command. ## Example #!/bin/bash while : do echo-n"Enter a number between 1 and 5:" read aNum case$aNum in 1|2|3|4|5)echo"You entered $aNum!" ;; *)echo"You did not enter a number between 1 and 5! Game over" break ;; esac done Executing the above code, the output is: Enter a number between 1 and 5:3You entered 3!Enter a number between 1 and 5:7You did not enter a number between 1 and 5! Game over ### continue The continue command is similar to the break command, with one difference: it does not exit all loops, only the current loop. Modify the above example: ## Example #!/bin/bash while : do echo-n"Enter a number between 1 and 5: " read aNum case$aNum in 1|2|3|4|5)echo"You entered $aNum!" ;; *)echo"You did not enter a number between 1 and 5!" continue echo"Game over" ;; esac done Running the code, you will find that when a number greater than 5 is entered, the loop in this example does not end, and the statement **echo "Game over"** is never executed.
← Docker Compose Start CommandMarkdown Tools and Editors β†’