"Focus" Shell Getting Started Tutorial: Process Control (3) The true and false value of conditional judgment

Source: Internet
Author: User

As mentioned before, what is true and false in bash is judged by whether the end state of the command is zero. Return 0, that is true, return non 0, that is false.

In bash, this is a formula that can affect a program's flow, called a conditional judgment . The operation number of judging type is divided into "unit" and "two Yuan" two kinds. such as "-F file" can test the existence of a file, operator-F followed by an operand "file", which is called "unit", such as "parameter 1-GT parameter 2" can test "parameter 1" value is greater than "parameter 2", the operator-gt the left and right of a parameter to compare, this judgment is called "two Yuan". Most "unit" judgments are used to determine the relative properties of a file, with a few options for testing strings and bash.

1. How to determine the attributes of a file

Bash's conditional judgment on file attributes, as in the following table:

Serial Number Judging Type under what circumstances is true
1 -A file If the file exists
2 -B File If the file exists and the file is a zone device file
3 -C file If the file exists and the file is a character device file
4 - D File If the file exists and the file is a directory
5 - e file If the file exists (note that it may be a directory name)
6 - F File If the file exists and the file is a generic file
7 -G file If the file exists and the file has a set group ID attribute
8 -H file If the file exists and the file is a symbolic link file
9 -K File If the file exists and the file has a sticky-bit attribute
10 -P File If the file exists and the file is a pipe file (FIFO)
11 -R File If the file exists and the file has a readable property
12 -S file If the file exists and the file size is greater than 0
13 -T file descriptor If the file descriptor is open and a terminal is linked
14 -U file If the file exists and the file has the Set User ID attribute
15 - w file If the file exists and the file can be written to a property
16 - x file If the file exists and the file has an executable property
17 -O File If the file exists and the file is owned by a valid user ID
18 -G file If the file exists and the file is owned by a valid group ID
19 -L File If the file exists and the file is a symbolic link file
20 -S file If the file exists and the file is a socket file
21st -N File If the file exists and the file has been modified since it was last read
22 Files 1-nt Files 2 If file 1 is newer than file 2, or file 1 exists, but file 2 does not exist
23 Files 1-ot Files 2 If file 1 is older than file 2, or file 2 exists, but file 1 does not exist
24 Files 1-ef Files 2 If file 1 and file 2 refer to the same device and inode number

Example:

#!/bin/bash[-E "/etc/hosts" | | (The echo '/etc/hosts file does not exist. '; exit 1) If ["$?"-eq 1]; Thenexitfiecho '/etc/hosts file exists, subsequent processing continues ... '

Line 3, use-E to determine whether/etc/hosts this file exists, if it does not exist, then () open a word shell, display the file does not exist information, and then return the left State value 1.

Line 4, if leave status is 1, end script.

2. Conditional judgment on strings

Bash's conditional judgment on strings, as in the following table:

Serial Number Judging Type under what circumstances is true
1 -Z String If the string length is 0 (that is, an empty string)
2 -N String If the string length is not 0 (that is, a non-empty string)
3 String If the string is not 0
4 String 1 = = String 2 If two strings are equal
5 String 1 = string 2 If two strings are equal
6 String 1! = String 2 If two strings are not the same
7 String 1 < String 2 If string 1 is less than String 2
8 String 1 > String 2 If string 1 is greater than String 2

Special Note: If < and > appear in the [] or test command, to escape the meaning of special characters with \, that is to write: [string 1 \< String 2] and [string 1 \> string 2], but if it appears in [[]], it is not necessary.

#!/bin/bashif ["$LOGNAME"! = "root"]; Thenecho ' This program must be performed with root privileges. ' Exit 1fiecho ' is now executing this program with root privileges. '

When comparing strings, it is a good idea to enclose the variable names in double quotation marks to avoid syntax errors when the contents of the variables are empty. The following example is a bad notation:

#!/bin/bashname=$1if [$NAME = "Joy"]; Thenecho ' You're joy ' elseecho ' You're not joy ' fi

Line 5, if the value of the variable $name is empty, causes the IF syntax not to complete the error:

if [= "Joy"]; Then

The correct wording:

#!/bin/bashname=$1if ["$NAME" = "Joy"]; Thenecho ' You're joy ' elseecho ' You're not joy ' fi

In addition, to improve portability, script can be executed in other shell environments, such as the traditional Boune shell, which uses the following techniques to determine whether strings are equal in order to avoid syntax errors by NULL variables:

#!/bin/bashname=$1if [x "$NAME" = X "Joy"]; Thenecho ' You're joy ' elseecho ' You're not joy ' fi

Line 5, if $NAME is empty, the equation becomes:

if [x = X "JOY"]; Then

This ensures that the syntax is complete. The X here can be replaced by any other English character.

3. Conditional judgment on formulas

The so-called calculation, where the value or the result of the operation is a numeric value (such as integers, non-strings)

Serial Number Judging Type under what circumstances is true
1 Parameter 1-eq parameter 2 The values for parameter 1 and parameter 2 are equal
2 Parameter 1-ne parameter 2 The values for parameter 1 and parameter 2 are not equal
3 Parameter 1-lt parameter 2 The value of parameter 1 is less than parameter 2
4 Parameter 1-le parameter 2 The value of parameter 1 is less than or equal to parameter 2
5 Parameter 1-GT parameter 2 The value of parameter 1 is greater than parameter 2
6 Parameter 1-ge parameter 2 The value of parameter 1 is greater than or equal to parameter 2

Example:

#!/bin/bashdeclare i lenlen=$1if [$len-eq 100]; Thenecho ' length of ' elseecho ' length is not 100, but $len "fi

Row 4, the command-line positional parameter, gets the value of Len's variable.

4. Conditional Judgment on bash options

Serial Number Judging Type under what circumstances is true
1 -O SET option name If this option is enabled

Example:

#!/bin/bashset-oif [-O history]; Thenecho ' bash option history open ' elseecho ' bash option history off ' fi

Line 3, use the SET command line to select options in the current shell.

Line 5 to determine if the history option is turned on.

"Focus" Shell Getting Started Tutorial: Process Control (3) The true and false value of conditional judgment

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.