Conditional testing and signal capturing

Source: Internet
Author: User

I. Exit

In Linux, whether it is a command, script, or program, the exit status exists after the execution is completed and the exit status is usually saved in the pre-defined variable $? . In most cases, the predefined variable only uses two numbers to indicate the exit status of the command, script, and program.

The number 0 indicates that the command, script, or program has been successfully executed and no error has occurred.

Number 1 indicates that some errors occurred during execution and failed to be executed.

Because the exit status is defined by the program writer, the exit status may not be 0 or 1. In this case, you may need to check the description file to understand the specific meaning.

Note: although the exit status may have many values, as long as the exit status is not 0, you can think that the program or command has an error.

[Exit status setting command]

When writing a complicated script, you should consider the error capture mechanism, that is, when the statement execution in the script encounters an error, the script can handle the error. A simple example: if the user does not provide necessary parameters using the script, the script should be able to check this error and prompt the user.

To set the exit status in a script, you need to use the exit command. The common usage and meanings of the command are as follows:

Exit 0: indicates that the script is successfully executed and no error is returned. This is sometimes called true ).

Exit 1: execution failed. An error is returned. In this case, it is also called false ).

In addition to the preceding values 0 and 1, you can use other numbers. However, if the returned status is not 0, the system considers the script execution failed.

When you use the exit command to set the exit status, you must note that, no matter where the script is executed, the script will immediately set the exit status and exit the script.

Note: The difference between the return and exit commands of the function return STATUS Command is that exit the program directly without executing the return command. The return command is only used for returning the function and will continue to be executed later.

Ii. File Testing

The file test includes two aspects: (1) Basic file test, including whether the file, directory exists, file type, and file length; (2) File Permission test, including whether the file is readable, writable, and executable.

1. Basic file test

Most of the basic file tests are used before creating files and directories. The purpose of this test is to make the script have better fault tolerance.

[File test command]

Common commands for file basic testing and their meanings are as follows:

D: test whether the target exists and is a directory.

F: test whether the target file exists and is a common file.

L: test whether the target file exists and is a linked file.

B: test whether the target file exists and is a block device file.

C: test whether the target file exists and is a character device file.

E: test whether the specified file or directory exists.

S: test whether the target file exists and is a Socket file.

P: test whether the target file exists and is a FIFO (pipe) file.

[File test Command Format]

[ -command parameter ]

In the basic format above, command is the test command, and parameter is the target file or directory to be tested. Note that the commands and parameters for file testing must be enclosed in brackets.

Note: The conditional test command can be used at a command prompt.

Note: similar to the C language, the shell script also uses numbers 0 to indicate true, rather than 0 to indicate false.

2. File Permission Test

[File Permission test command]

Common File Permission test commands and their meanings are as follows:

W: determines whether the specified file exists and has the write permission.

R: determines whether the specified file exists and has the readable permission.

X: determines whether the target file exists and has executable permissions.

U: determines whether the target file exists and has the SUID attribute.

G: determines whether the target file exists and has the SGID attribute.

K: determines whether the target file exists and has the Sticky bit attribute.

S: determines whether the target file exists and is not a blank file.

Iii. String and numerical Testing

1. String Testing

[Operators and commands for string testing]

The operators used for string testing and their meanings are as follows:

=: Determines whether two strings are equal. If they are equal, true (0) is returned ).

! =: Determines whether two strings are not equal. If the two strings are not equal, true is returned.

Z: test whether the string is null (the length is 0). If it is null, return true.

N: test whether the string is non-empty. If it is not null, the return value is true.

2. Numerical Test

[Numeric test operator]

Eq: returns true if two numbers are equal.

Ne: returns true if two numbers are not equal.

Lt: if the number of 1st instances is less than 2nd, the return value is true.

Le: returns true if the number of 1st is less than or equal to 2nd.

Gt: returns true if the number of 1st instances is greater than 2nd.

Ge: returns true if the number of 1st is greater than or equal to 2nd.

When you use the preceding command to perform a numerical test, you can also use "=" and "! =.

Iv. logical operators

[Common logical operators]

A: The logic and operator are true on both sides, and the result is true. Otherwise, the result is false.

O: either logic or operator is true, and the result is true. Otherwise, the result is false.

! : Non-logical.

V. Capture System Signals

When writing a script, you may be worried that the Shell script will stop running because the script user sends a signal to terminate the script, and thus cannot complete the work being processed. If the script is processing some insignificant work, termination of the script may not cause any loss. However, if a very important task is being executed, the termination of the script may cause losses. Therefore, in important scripts, the termination signal sent by the system should be captured and corresponding actions should be taken to avoid unnecessary losses caused by script termination.

[Captured signal]

Generally, the number, name, and meaning of the captured signal are as follows:

1 (SIGHUP): The death signal from the control terminal or from the control process.

2 (SIGINT): the interrupt signal from the keyboard.

3 (SIGQUIT): Exit signal from the keyboard.

15 (SIGTERM): The termination signal.

If you are not sure about the signal that the script will capture, we recommend that you capture all the signals that can be captured in important scripts to avoid unnecessary losses.

[Measures after signal capturing]

After the system signal is captured, the script should take immediate action. Generally, the following three measures can be taken:

Do not capture signals. The system processes these signals. This will cause the execution of the script to end, pause, and other consequences. This method is usually used in unimportant scripts.

Capture but ignore the signal, that is, capture to the back of the signal to take any action.

Capture signals and take actions, which are usually used in important scripts. The operation usually prompts the user whether to interrupt the execution of the script or send a message to the user that the script is being executed and cannot exit.

[Capture signal format]

Use the command trap to capture system signals. The basic format is as follows:

Trap "command" signals

In the preceding format, command indicates the command or function that needs to be executed after the signal is captured. If it is null (expressed by ""), the signal is ignored; otherwise, the corresponding measures are taken to process the signal. Signals indicates the list of signals to be captured. It can be represented by numbers or by signal names. Multiple signals are separated by spaces.

[Usage example]

In the following sample script, the end signal sent by the user from the keyboard is to be captured (the case is Ctrl + C, and the corresponding signal is 2 ), after the signal is captured, the script will call the function trap, output the prompt information, and exit:

!/bin/-- -n -n 

Running result:

[root@localhost shell]# ./+->->->

Press the shortcut key Ctrl + C, you can see that the script captures the interrupt signal sent by the user from the keyboard, and calls the trap function, displays the prompt information and waits for 3 seconds to exit.

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.