Process Control for Shell Program Design

Source: Internet
Author: User
ShellProgramDesign Process Control

Like other advanced programming languages, shell provides commands used to control program execution processes, including conditional branches and loop structures. You can use these commands to create very complex programs.
Unlike traditional languages, shell is used to specify condition values instead of Boolean operations, but commands and strings.

1. Test command

The test command is used to check whether a condition is true. It can be used to test values, characters, and files. The test characters and corresponding functions are as follows.

(1) numerical test:
-If EQ is equal to, it is true.
-If the ne value is not equal to the ne value, it is true.
-A value greater than-GT is true.
-If Ge is greater than or equal to, it is true.
-If it is less than, it is true.
-If the value of-Le is less than or equal to, it is true.

(2) string test:
= Equals to true.
! = Not equal to true.
The-Z string length is false.
-If the length of the N string is not pseudo, it is true.

(3) file test:
-E: the file name is true if the file exists.
-R: The file name is true if the file exists and is readable.
-W: The file name is true if the file exists and can be written.
-X: the file name is true if the file exists and can be executed.
-S file name is true if the file exists and contains at least one character.
-D. The file name is true if the file exists and is a directory.
-F: The file name is true if the file exists and is a normal file.
-C: The file name is true if the file exists and is a special character file.
-B: The file name is true if the file exists and is a special file.

In addition, Linux also provides (!) , Or (-O), non-(-a) logical operators, used to connect test conditions, the priority is :! Highest, followed by-A and-o.

Bash can also perform simple arithmetic operations in the following format:

$ [Expression]

For example:
Var1 = 2
Var2 = $[var1*10 + 1]
Then the value of var2 is 21.

2. If Condition Statement

The condition branch in the shell program is implemented through the IF Condition Statement. The general format is:

If condition command string
Then
Command string when the condition is true
Else
Command string when the condition is false
Fi

3. For Loop

The for loop executes a command sequence for all possible values of a variable. The values assigned to the variable can be provided in the form of a numerical list in the program, or in the form of a positional parameter outside the program. The general format of the For Loop is:

For variable name [in Value List]
Do
Several command lines
Done

The variable name can be any string selected by the user. If the variable name is Var, the value given after in replaces $ VaR in the circular command list in sequence. If in is omitted, the value of VaR is the location parameter. Every possible value assignment to a variable will execute the command list between do and done.

4. While and until Loops

Both the while and until commands use the return status value of the command to control the loop. The general format of the while loop is:

While several command lines 1
Do
Several command lines 2
Done

As long as the return status of the last command in "Several command line 1" in while is true, the while loop continues to execute "Several command line 2" between do... done ".

The until command is another loop structure, which is similar to the while command in the following format:

Until several command lines 1
Do
Several command lines 2
Done

The difference between an until loop and a while loop is that a while loop continues to run when the condition is true, while an until loop continues to run when the condition is false.

Shell also provides the "true" and "false" commands to create an infinite loop structure. Their return states are always 0 or not 0.

5. Select case conditions

The IF Condition Statement is used to select one of the two options, and the case condition selection provides you with a method to select one from multiple options based on the value of the string or variable. The format is as follows:

Case string in
Exp-1)
Several command lines 1
;;
Exp-2)
Several command lines 2
;;
......
*)
Other command lines
Esac

By calculating the string value, shell compares the results in sequence with the formula exp-1, exp-2, etc. until a matching formula is found. If a match is found, run the following command until a semicolon (;) is encountered.

You can also use shell wildcards ("*", "?" , "[]"). * Is usually used as the final formula of the Case command to execute the "other command line" command if no matching item is found.

6. Unconditional control statements break and continue

Break is used to terminate the execution of the current loop immediately, while contiune is used to start the execution of the next loop immediately without executing the statements following the loop. These two statements are valid only when they are placed between do and done.

7. Function Definition

You can also define functions in shell. A function is actually composed of several shell commands, so it is similar to a shell program in form. The difference is that it is not a separate process, but a part of the shell program. The basic format of the function definition is:

Functionname

{

Several command lines

}

The format of the called function is:

Functionname param1 param2...

Shell functions can complete some routine work and have their own exit States. Therefore, functions can also be used as conditions for control structures such as if and while.

Parameters are not required for function definition, but can be included when a function is called. In this case, shell will assign these parameters to the corresponding location parameters $1, $2 ,... and $ *.

8. Command Group

There are two command grouping methods in shell: () and {}. The former creates a new sub-process when the shell executes the commands in (), and then the sub-process executes the commands in the circular arc. When a user does not want to change the status set (such as location parameters, environment variables, and current working directory) during command execution to affect the execution of the following statements, these commands should be placed in the circular arc to ensure that all changes only affect sub-processes, and the parent process is not disturbed. {} Is used to use the output results of the commands executed in sequence for the input (pipe mode) of another command ). When we really want to use circular arc and curly arc (such as computing formula priority), we need to add an escape character (\) before it (\) so that the shell knows that they are not used for command execution control.

9. Signal

The trap command is used to capture signals in Shell programs. There are three response methods:

(1) execute a program to process this signal.
(2) receive the default signal operation.
(3) Ignore this signal.

Trap provides three basic forms for the above three methods:

The first form of Trap Command will execute the command string in double quotation marks when shell receives the signal with the same value as the signal list.

Trap 'commands' signal-list

Trap "commands" signal-list

To restore the default signal, use the second form of trap command:

Trap signal-list

The third form of the trap command allows the ignore signal:

Trap "" signal-list

Note:

(1) The signal 11 (segment violation) cannot be captured, because the shell itself needs to capture the signal for memory dump.

(2) In the trap, you can define the processing of the signal 0 (in fact, there is no such signal). The shell program sends this signal when it terminates (such as executing the exit Statement.

(3) After capturing the signal specified in the signal-list and executing the corresponding commands, if these commands do not terminate the shell program, the shell program will continue to execute the command after the command received when the signal is received, which will easily cause the shell program to be unable to terminate.

In TRAP statements, single quotes and double quotation marks are different. When the shell program first encounters a trap statement, it will scan the commands in commands. If commands is enclosed in single quotes, shell will not replace the variables and commands in commands. Otherwise, the variables and commands in commands will be replaced with the specific values at that time.

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.