Shell basic Syntax (ii)

Source: Internet
Author: User
Tags syslog

If statementBasic format

Format 1:if condition; Then statement; Fi

Format 2:if condition; Then statement; else statement; Fi

Format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi


can use && | | Combine multiple conditions

If [$a-gt 5] && [$a-lt 10]; Then

If [$b-gt 5] | | [$b-lt 3]; Then


Arithmetic comparison operators

Num1-eq num2 equals [3-eq $mynum]

Num1-ne num2 Not equal to [3-ne $mynum]

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

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

NUM1-GT num2 greater than [3-GT $mynum]

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


The judgment of the document

-e filename true if filename exists [-e/var/log/syslog]

-D filename True if filename is a directory [-d/tmp/mydir]

-F filename True if filename is a regular file [-f/usr/bin/grep]

-L filename True if filename is a symbolic link [-l/usr/bin/grep]

-R filename True if filename is readable [-r/var/log/syslog]

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

-X filename is true if filename is executable [-l/usr/bin/grep]


Special usage

If [-Z ' $a '] This indicates what happens when the value of variable A is empty (develop a good habit, be sure to add "double quotation marks" to the value of the judgment; if "file" can be omitted)

If [-n ' $a '] means that the value of variable A is not empty

If Grep-q ' 123 ' 1.txt; Then what happens if the 1.txt contains a ' 123 ' row

if [!-e file]; Then what happens when the file doesn't exist?

if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ...

Symbols such as <,>,==,!=,>=,<= cannot be used in []


# if Grep-wq ' Juispan '/etc/passwd; Then echo "Juispan already exists"; Fi

Juispan already exists


Case StatementBasic format

Case variable name in

value1)

Command

;;

value2)

Command

;;

*)

Commond

;;

Esac


If one of the value in the case is the same, you can also write this:


2|3) # # "|" The meaning of an expression or

Command

;;


Shell basic Syntax (ii)

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.