Shell script Management in Linux (ii)

Source: Internet
Author: User
Tags comparison execution parent directory disk usage backup

The benefits of using an if statement in a shell script are: You can decide whether to perform an action based on a specific condition (eg: determining whether the backup directory exists), and perform different actions when different conditions are met (eg: if the backup directory does not exist, the directory is created or the operation is skipped). In this paper, the application of the IF statement in the shell script is explained from the conditional test action, the IF statement structure and the example three aspects.

1, the condition test operation:

The first problem you need to selectively perform tasks in your shell script is how do you set the conditions for the execution of the command?

In a shell environment, you can determine whether the command was executed successfully by returning a status value based on the execution of the command, or if the return value of 0 is a successful execution, otherwise (a value other than 0) indicates execution failure. When testing for a particular conditional expression, you can use the specialized tools provided in the Linux system to--test commands,

There are two types of tests that you can use when testing a command.

Test condition expression

The conditional expression

The two methods work exactly the same, but often the latter form is more commonly used and is closer to the programming habit. Note that the brackets "[" or "]" need at least one space to be delimited between the conditional expression statements.

According to the need to judge the conditions of the content of different, conditional operation is also different, the most commonly used conditions include file state testing, comparing the integer value size, Comparison of strings, and at the same time to determine the logic of multiple conditions, the following will be explained respectively. The following is mainly a test form of square brackets.

1.1. test file status

The file state test refers to whether the name corresponds to a file or directory according to the given path name, or to determine if the file is readable, writable, executed, etc. Depending on the state of the judgment, you need to use different action options in the conditional expression.

-D: Test for Directory (directory).

-E: Test whether the directory or file exists (Exist).

-F: Test for file.

-R: Tests whether the current user has permission to read (read).

-W: Tests whether the current user has permission to write (write).

-X: Tests whether the current user can perform (excute) the file.

-L: Tests whether a symbolic connection (link) file.

After the conditional test operation is performed, the predefined variable "$?" is used. The return status value of the test command can be obtained to determine whether the condition is valid (the return of a value of 0 indicates that the condition is set, and a value other than 0 indicates that the condition is not tenable). But looking at the test results in this way is cumbersome.

Example 1: Test if "/etc/hosts" is a file and pass "$?" Variable to view the return status value, which determines the test result.

[-f/etc/hosts]
echo $?
0   //The return value is 0, which means the condition of the previous test is set.

Example 2: Test for the existence of "/media/cdrom/server" and its parent directory, or "YES" if present, without outputting any information.

[-e/media/cdrom/server] && echo "yes"
                                                                        //No output indicates that the directory does not exist
[-e/media/cdrom] && echo "yes"

1.2, the whole numerical comparison:

The whole numerical comparison is to determine whether the first number is greater than or equal to or less than the given two integer values. Number 2nd, the following action options are available:

-eq: The first number equals (Equal) the second number.
-ne: The first number is not equal to (not Equal) the second number.
-GT: The first number is greater than (Greater Than) the second number.
-LT: The first number is less than the second number (lesser Than).
-le: The first number is less than or equal to the second number (lesser or Equal).
-ge: The first number is greater than or equal to (Greater or Equal) the second number.

The test operations for integer numeric comparisons are more widely used in shell scripting, such as determining disk usage, the number of logged-in users, and the number of loops used to control script statements.

Example 1: Test whether the number of users currently logged on to the system is less than or equal to 10, and the output is "yes".

who | Wc-l
5
[' Who | wc-l '-le] && echo "yes"

This column more highlights: http://www.bianceng.cn/OS/Linux/

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.