Shell Process Control and shell Process

Source: Internet
Author: User

Shell Process Control and shell Process

Unlike Java, PHP, and other languages, sh process control cannot be blank

If elseif statement syntax format:
if conditionthen    command1     command2    ...    commandN fi
Write a line (applicable to terminal command prompt ):
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
The fi at the end is the spelling of if, and the following examples will be used to determine whether two variables are equal:
A = 10b = 20if [$ a = $ B] then echo "a equals B" elif [$ a-gt $ B] then echo "a greater than B" elif [$ -lt $ B] then echo "a less than B" else echo "no matching condition" fi
Output result:
A is less than B
The general format of for loop is:
for var in item1 item2 ... itemNdo    command1    command2    ...    commandNdone
Write a line:
for var in item1 item2 ... itemN; do command1; command2… done;
When the variable value is in the list, the for loop executes all the commands once and obtains the current value in the list using the variable name. Command can be any valid shell command and statement. The in list can contain replacement, string, and file name. The in list is optional. If you do not need it, the for loop uses the location parameter of the command line. For example, output the numbers in the current list sequentially:
for loop in 1 2 3 4 5do    echo "The value is: $loop"done
Output result:
The value is: 1The value is: 2The value is: 3The value is: 4The value is: 5
Characters in the output string in sequence:
for str in 'This is a string'do    echo $strdone
Output result:
This is a string
The while statement while loop is used to continuously execute a series of commands and read data from the input file. commands are usually used as test conditions. The format is:
while conditiondo    commanddone
The following is a basic while loop. the test condition is: if the int value is less than or equal to 5, the condition returns true. Int starts from 0, and 1 is added to int for each loop processing. Run the preceding script, return numbers 1 to 5, and then terminate.
#!/bin/shint=1while(( $int<=5 ))do  echo $int  let "int++"done
Run the script and output:
12345
The Bash let command is used to execute one or more expressions. You do not need to add $ in variable calculation to indicate that the while loop variable can be used to read keyboard information. In the following example, the input information is set to the variable FILM, and the loop is ended by pressing <Ctrl-D>.
Echo 'Press <CTRL-D> to exit 'echo-n' and enter your favorite movie name: 'while read FILMdo echo "Yes! $ FILM is a good movie "done
Run the script. The output is similar to the following:
Press <CTRL-D> to exit and enter your favorite movie name: frethers yes! Frethers is a good movie.
Infinite Loop syntax format:
While: do commanddone # Or while truedo commanddone # Or ((;;))
Until loop executes a series of commands until the condition is true. The Processing Method of the until loop is the opposite to that of the while loop. The while loop is generally better than the until loop, but in some cases-in rare cases, the until loop is more useful. Until syntax format:
until conditiondo    commanddone
The condition can be any test condition. The test occurs at the end of the loop, so the loop must be executed at least once. Please note this. CaseShell case statements are multiple select statements. You can use the case statement to match a value with a pattern. If the match succeeds, execute the matched command. The case statement format is as follows:
Case value in Mode 1) command1 command2... commandN; Mode 2) command1 command2... commandN; esac
The case method is shown in the preceding figure. The value must be followed by the word in, and each pattern must end with parentheses. The value can be a variable or constant. After matching finds that the value matches a certain mode, all commands are executed ;;. The value will detect each matching mode. Once the mode matches, the matching mode command is executed and the other modes are not continued. If no matching mode exists, use asterisk * to capture the value and then execute the following command. The following script prompts you to enter 1 to 4 to match each mode:
Echo: numbers between 1 and 4: 'echo 'the number you entered is: 'read aNumcase $ aNum in 1) echo' you selected 1'; 2) echo 'you selected 2'; 3) echo' you selected 3'; 4) echo 'you selected 4 ';;*) echo 'you have not entered a number between 1 and 4 '; esac
Input different content has different results, such:
Enter a number between 1 and 4: The number you entered is: 3. You selected 3.
In the loop process, the loop jumps out forcibly when the loop end condition is not reached. Shell uses two commands to implement this function: break and continue. The break command can jump out of all loops (all loops following the termination of execution ). In the following example, the script enters an endless loop until the number entered by the user is greater than 5. To jump out of this loop and return to the shell prompt, use the break command.
#! /Bin/bashwhile: do echo-n "enter a number from 1 to 5:" read aNum case $ aNum in 1 | 2 | 3 | 4 | 5) echo "the number you entered is $ aNum! "; *) Echo" the number you entered is not between 1 and 5! Game ended "break; esacdone
Run the above Code and the output result is:
Enter a number between 1 and 5: 3. The number you entered is 3! Enter a number between 1 and 5: 7 The number you entered is not between 1 and 5! Game ended
The continue command is similar to the break command. There is only one difference. It does not jump out of all loops and only jumps out of the current loop. Modify the preceding example:
#! /Bin/bashwhile: do echo-n "enter a number from 1 to 5:" read aNum case $ aNum in 1 | 2 | 3 | 4 | 5) echo "the number you entered is $ aNum! "; *) Echo" the number you entered is not between 1 and 5! "Continue echo" game ends "; esacdone
Run the code to find that when you enter a number greater than 5, the loop in this example will not end, and the statement echo "Game is over! "Will never be executed. The syntax of case is very different from that of C family. It requires an esac (that is, the case in turn) as the end mark. Each case Branch uses the right parentheses and two semicolons (;) to represent the break.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.