12-shell Process Control

Source: Internet
Author: User
Tags case statement closing tag

You can't write this in Sh/bash, and if the Else branch doesn't have a statement execution, don't write this else.

If Else

if
If statement syntax format:
If condition
Then
Command1
Command2
...
CommandN
Fi
Write a line (for the terminal command prompt):
If [$ (ps-ef | grep-c "SSH")-gt 1]; Then echo "true"; Fi
At the end of the fi is if the upside-down spelling, and later will encounter similar.

if Else
If ELSE syntax format:
If condition
Then
Command1
Command2
...
CommandN
Else
Command
Fi

if else-if else
If else-if else syntax format:
If Condition1
Then
Command1
   elifCondition2
Then
Command2
Else
CommandN
Fi
The following example determines whether two variables are equal:
a=10
B=20
if [$a = = $b]
Then
echo "a equals B"
   elif[$a-gt $b]
Then
echo "A greater than B"
   elif[$a-lt $b]
Then
echo "A is less than B"
Else
echo "No conditions met."
Fi
Output Result:
A is less than B
The If Else statement is often used in conjunction with the Test command, as follows:
NUM1=$[2*3]
NUM2=$[1+5]
IfTest$[NUM1]-eq $[num2]
Then
Echo ' Two numbers equal! '
Else
Echo ' Two numbers are not equal! '
Fi
Output Result:
Two numbers equal!

For Loop
Similar to other programming languages, the shell supports A for loop.
The general format for A for loop is:
For Var in item1 item2 ... itemn
Do
Command1
Command2
...
CommandN
Done
Write a line:
For Var in item1 item2 ... itemn; Do Command1; Command2. Done;
When the value of the variable is in the list, the For loop executes all commands once, using the variable name to get the current value in the list. The command can be any valid shell command and statement. The in list can contain replacements, strings, and file names.
The in list is optional, if it is not used, the For loop uses the command-line positional parameters.
For example, sequentially output the numbers in the current list:
For loop in 1 2 3 4 5
Do
echo "The value is: $loop"
Done
Output Result:
The value is:1
The value Is:2
The value Is:3
The value Is:4
The value Is:5
Characters in the sequential output string:
For str ' A string '
Do
Echo $str
Done
Output Result:
This is a string

While statement
The while loop is used to continuously execute a series of commands and to read data from the input file; the command is usually a test condition. The format is:
While condition
Do
Command
Done
The following is a basic while loop, and the test condition is: If int is less than or equal to 5, then the condition returns True. int starts at 0, and each time the loop is processed, int plus 1. Run the above script, return the number 1 to 5, and then terminate.
   #!/bin/bash
Int=1
While(($int <=5 ))
Do
Echo $int
Let "int++"
Done
Run script, Output:
1
2
3
4
5
Using the Bash let command in use, which executes one or more expressions, the variable calculation does not need to add $ to represent the variable, which can be viewed in detail: Bash lets command.
The while loop can be used to read keyboard information. In the following example, the input information is set to the variable film, ending the loop by <Ctrl-D>.
Echo ' Press <CTRL-D> exit '
Echo-n ' Enter your favorite website name: '
While Read FILM
Do
echo "Yes! $FILM is a good website "
Done
Run the script with the output similar to the following:
Press <CTRL-D> exit
Enter your favorite site name: Beginner's Tutorial
Yes! Rookie Tutorial is a good site

Infinite Loops
Infinite loop Syntax format:
   While :
Do
Command
Done
Or
   While true
Do
Command
Done
Or
   For ((;;))

until cycle
The Until loop executes a series of commands until the condition is true.
The Until loop and the while loop are just the opposite of the processing mode.
The general while loop is better than the until loop, but at some point-and only in rare cases-the until loop is more useful.

Until syntax format:
Until condition
Do
Command
Done
condition is generally a conditional expression, and if the return value is false, the statement in the loop body continues to execute, otherwise it jumps out of the loop.
The following example uses the until command to output a 0 to 9 number:
   #!/bin/bash
A=0
Until [! $a-lt 10]
Do
Echo $a
A= ' expr $a + 1 '
Done
Operation Result:
The output is:
0
1
2
3
4
5
6
7
8
9

Case
The Shell Case statement is a multi-select statement. A case statement can be used to match a value with a pattern, and if the match succeeds, the matching command is executed. The case statement is in the following format:
Case value in
Mode 1)
Command1
Command2
...
CommandN
;;
Mode 2)
Command1
Command2
...
CommandN
;;
Esac
The case works as shown above. The value must be followed by the word in, and each pattern must end with a closing parenthesis. The value can be either a variable or a constant. After the match discovery value conforms to a pattern, all commands begin to execute until;;.
The value will detect each pattern that matches. Once the pattern matches, the other mode is no longer resumed after the corresponding command is executed.If there is no matching pattern, use an asterisk * to capture the value and then execute the subsequent command.
The following script prompts you to enter 1 to 4 to match each pattern:
Echo ' Enter a number from 1 to 4: '
Echo ' The number you entered is: '
Read Anum
Case $aNum in
1) echo ' You have chosen 1 '
;;
2) echo ' You have chosen 2 '
;;
3) echo ' You have chosen 3 '
;;
4) echo ' You have chosen 4 '
;;
*) echo ' You did not enter a number between 1 and 4 '
;;
Esac
Enter different content, there will be different results, for example:
Enter a number from 1 to 4:
The number you entered is:
3
You chose 3.

Jump out of the loop
In the loop, sometimes you need to force out of the loop when the loop end condition is not reached, and the shell uses two commands to implement the function: Break and continue.
Break Command
The break command allows you to jump out of all loops (all loops after the execution is terminated).
In the following example, the script enters a dead loop until the user enters a number greater than 5. To jump out of this loop and return to the shell prompt, you need to use the break command.
#!/bin/bash
While:
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 Over "
Break
;;
Esac
Done
Execute the above code and the output is:
Enter a number from 1 to 5:3
The number you entered is 3!
Enter a number from 1 to 5:7
The number you entered is not between 1 and 5! Game Over

Continue
The continue command is similar to the break command, with only a little difference, and it does not jump out of all loops and just jumps out of the current loop.
To modify the above example:
   #!/bin/bash
While:
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 Over"
;;
Esac
Done
Running code discovery, when a number greater than 5 is entered, the loop in the example does not end, and the statement echo "Game Over" is never executed.

Esac
The syntax of the case differs greatly from the C family language, which requires a ESAC (which is case, in turn) as the closing tag, each case branch with a closing parenthesis and a two semicolon for break.




















































12-shell Process Control

Related Article

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.