Powerful shell: If Condition Statement

Source: Internet
Author: User
Tags syslog

If statement test condition. After the test condition returns true (0) or false (1), you can execute a series of statements accordingly. If statement structure is very useful for error checks. The format is:
If condition 1
Then command 1
Elif condition 2
Then command 2
Else
Command 3
Fi

The IF statement must be terminated by word fi. FI is the most common error in if statements. I sometimes do the same. Elif and else are optional. If the statement does not contain the otherwise part, The Elif and else sections are not required. The IF statement can have many Elif parts.

Here is an example:
[Root @ localhost/] # vi IFCP. Sh
#! /Bin/sh

If CP myfile. Chen; then
Echo "Good Copy"
Else
Echo "$0: Error cocould not copy the files"
Fi

[Root @ localhost test] # Sh./IFCP. Sh
CP: cannot stat 'myfile': no such file or directory
./IFCP. sh: Error cocould not copy the files

We do not have the myfile file. Of course we are on a business trip. Create an object:
[Root @ localhost test] # echo "I love zengshuai"> myfile
[Root @ localhost test] # Sh./IFCP. Sh
Good copy

Note that $ # indicates the number of command line parameters, including $0.
In shell, the script name is $0. Here we are "./IFCP. Sh", and the rest are $1, $2... , $ {10}, $ {11}, and so on.
$ * Indicates the entire parameter list, excluding $0, that is, the parameter list excluding the file name.

 

However, we generally use a more powerful if condition judgment in shell, that is, the if [] format. The test options in the IF [] format are attached below:

 

-B. returns true if the file exists and is a block file.

-C: returns true if the file exists and is a character file.

-D. If pathname exists and is a directory, true is returned.

-E: returns true if the file or directory specified by pathname exists.

-F returns true if the file exists and is a regular file

-G returns true if the file or directory specified by pathname exists and the sgid bit is set.

-H returns true if the file exists and is a symbolic link file. This option is invalid in some old systems.

-K. If a file or directory specified by pathname exists and a "Sticky" bit is set, the system returns the true value.

-P: returns true if the file exists and is a command pipeline.

-R: returns true if the file or directory specified by pathname exists and is readable.

-S: returns true if the file size is greater than 0.

-U returns true if a file or directory specified by pathname exists and SUID is set.

-W returns true if the file or directory specified by pathname exists and is executable. A directory must be executable for its content access.

-O returns true if a file or directory specified by pathname exists and the user specified by the valid user ID of the current process is owned by it.

Comparison character writing in Unix shell:

-EQ equals

-Ne is not equal

-GT greater

-Lt is less

-Le is less than or equal

-Ge is greater than or equal

-Z empty string

= Two equal characters

! = Two characters

-N non-empty string

 

For more details:

Operator Description Example

File comparison operator

-E filename if filename exists, it is true [-E/var/log/syslog]

-D filename if filename is a directory, it is true [-D/tmp/mydir]

-F filename if filename is a regular file, it is true [-F/usr/bin/grep]

-L filename if filename is a symbolic link, it is true [-L/usr/bin/grep]

-R filename if filename is readable, it is true [-r/var/log/syslog]

-W filename if filename is writable, it is true [-W/var/mytmp.txt]

-X filename if filename is executable, it is true [-L/usr/bin/grep]

Filename1-nt filename2 if filename1 is newer than filename2, it is true [/tmp/install/etc/services-nt/etc/services]

Filename1-ot filename2 if filename1 is earlier than filename2, true [/boot/bzimage-ot ARCH/i386/boot/bzimage]

String comparison operator (please note the use of quotation marks, which is a good way to prevent space from disturbing the code)

 

-Z string if the string length is zero, it is true [-Z $ myvar]

-N string if the string length is non-zero, it is true [-N $ myvar]

String1 = string2 if string1 is the same as string2, it is true [$ myvar = One Two Three]

String1! = String2 if string1 is different from string2, it is true [$ myvar! = One Two Three]

Arithmetic comparison operator

Num1-EQ num2 equals to [3-EQ $ mynum]

Num1-ne num2 is not equal to [3-ne $ mynum]

Num1-lt num2 is less than [3-lt $ mynum]

Num1-Le num2 is less than or equal to [3-Le $ mynum]

Num1-GT num2 is greater than [3-GT $ mynum]

Num1-ge num2 is greater than or equal to [3-ge $ mynum]

 

 

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.