linux[basic]16-shell Judging user parameters [01]

Source: Internet
Author: User

Shell to determine user parameters

Conditional test statements are able to test the characteristics of an expression that is true

The return value is 0 when the condition is set

Otherwise, return other values

To test the statement format:

[ conditional expression ] There should be a space on both sides

The test statements are:

File test

Logic test

Integer value Comparison

string comparison

File test:

[ operator file or directory name ]

Parameters

Role

-D

Whether the test is a directory

-E

Test whether a file or directory exists

-F

Determine if the file is

-R

Tests whether the current user is authorized to read

-W

Test whether the current user can write

-X

Test whether the current user is executable

Instance:

[[Email protected] ~]# [-D/ETC] # to determine if it is a directory

[[email protected] ~]# echo $?

0

[[Email protected] ~]# [-f/etc/inittab] # to determine if it is a file

[[email protected] ~]# echo $?

0

[[Email protected] ~]# [-r/etc/inittab] # to determine if the current user is readable

[[email protected] ~]# echo $?

0

[[Email protected] ~]# [-w/etc/inittab] # Determine if the current user can write

[[email protected] ~]# echo $?

0

[[Email protected] ~]# [-x/etc/inittab] # Determine if the current user can execute

[[email protected] ~]# echo $?

1

Example 2:

[Email protected] ~]# [-e/dev/cdrom] && echo "Exist"

Exist

Logic test:

[ expression 1] operator [ expression 2]

Operator

Role

&&

Logical AND, "and" meaning

||

Logical OR, "or" of the above

!

Logic's No

Example 1:

Root User under:

[[Email protected] ~]# [$USER! = root] && echo "USER"

[Email protected] ~]#

Under normal User:

[[Email protected] ~]$ [$USER! = root] && echo "USER"

User

[Email protected] ~]$

Example 2:

Root User under:

[[Email protected] ~]# [$USER! = root] && echo "USER" | | echo "Root"

      Root

[Email protected] ~]#

Integer value comparison:

[ integer 1 operator integer 2]

Operator

Role

-eq

Determine if it equals

-gt

Judging if it is greater than

-lt

Judging if it is less than

-le

Determine if it equals less than

-ge

Determine if it equals greater than

Example 1:

[Email protected] ~]# [10-GT] # to determine if it is more than ten

[[email protected] ~]# echo $?

1 no

Example 2:

[Email protected] ~]# [10-eq] # to determine if it equals ten

[[email protected] ~]# echo $?

0 is

Example 3:

[Email protected] ~]# freemem= ' free-m | grep cache: | awk ' {print $} '

[Email protected] ~]# echo $FreeMem

205

[[Email protected] ~]# [$FreeMem-lt 1024x768] && echo "Out of memory "

Not enough memory

[Email protected] ~]#

string comparison:

[ string 1 operator string 2]

Operator

Role

=

Compare strings for the same content

!=

Compare strings for different content

-Z

Determines whether the string contents are empty

Example 1:

[[Email protected] ~]# [-Z $String] # Determine if the variable is empty

[[email protected] ~]# echo $?

0

Example 2:

[Email protected] ~]# [$LANG! = "en. US "] && echo" not en. US "

# determine if the system language is en.us

Not en. US

linux[basic]16-shell Judging user parameters [01]

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.