If judgment for Linux shell scripts

Source: Internet
Author: User

 

LinuxIf command
WhateverProgrammingLanguage is inseparable from conditional judgment. Shell is no exception.

If list then
Do something here
Elif list then
Do another thing here
Else
Do something else here
Fi

Ex1:

#! /Bin/sh

System = 'uname-S' # obtain the operating system type.

If [$ system = "Linux"]; then # If it is Linux, print the Linux string
Echo "Linux"
Elif [$ system = "FreeBSD"]; then
Echo "FreeBSD"
Elif [$ system = "Solaris"]; then
Echo "Solaris"
Else
Echo "what? "
FI # ifend

It is basically the same as other scripting languages. There is no big difference. However, it is worth noting that. [] Condition judgment.

1 string judgment

Str1 = str2: true when two strings have the same content and length
Str1! = Str2: true when str1 and str2 are different
-N str1: true when the length of a string is greater than 0 (the string is not empty)
-Z str1: true (empty string) when the string length is 0)
Str1 is true when str1 is not empty

2. digit judgment

Int1-EQ int2 two numbers are equal to true
Int1-ne int2 two numbers are not true
Int1-GT int2 int1 greater than int2 true
Int1-ge int2 int1 greater than or equal to int2 true
Int1-lt int2 int1 smaller than int2 true
Int1-Le int2 int1 smaller than or equal to int2 true

3. File judgment

-R file user readable to true
-W file users can write to true
-X file user executable is true
-F file: the regular file is true.
-D file: the directory is true.
-C file: the special character file is true.
-B file: The Block special file is true.
-S file is true if the file size is not 0
-T file: this parameter is true when the specified device is a terminal in the file descriptor (1 by default ).

3. complex logic judgment

-A and
-O or
! Non

End

Although the syntax is simple, its functions become powerful when used in shell.

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.