The use of parentheses in the shell Linux shell

Source: Internet
Author: User

Overview of the use of parentheses in the Linux shell
    本文主要介绍Linux中括号的用法,主要包含判断文件和变量。**注意事项**:中括号两边必须有空格。
The knowledge involved in using brackets &&, | | |
            在本文对文件或者变量做判断的时候,可能会多次使用到&&和||这两个符号,所以在我们进入中括号的学习之前首先了解一下这两个符号的作用。            &&:并 --> 两真才为真,如果有两个条件,第一个为真,那么肯定会判断第二个条件;如果第一个条件为假,那么第二个条件将不会判断。(第一个为真,那么需要判断第二个条件才知道整体结果是否为真,如果第一个条件为假,那么无论第二个条件为真还是假,最终的结果都是假)

Example 1:
[FALSE] && echo 1 final result no output, because the first condition is false, the back of the echo 0 will not execute

Example 2:
[true] && echo 0 The final result is 0, because one condition is true and the second condition is definitely executed.

            ||:且 -->一个为真即是真。如果第一个条件为真,那么不会判断第二个条件(因为一真即真),如果第一个条件为假,那么判断第二个条件(第二个条件如果为真还有机会使得整体的结果为真)

Example 3:
[True] | | Echo 0 # The final result has no output because the first condition is true and echo 0 will not execute.

Example 4:
[FALSE] | | echo 0 #最终的结果输出0

Little Practice
[2-eq 3] && echo 0
[2-eq 3] | | Echo 1
What is the result of the final output?

The result is: 1

&& | | Integrated use of

Let's look at a small example:
[2-eq 3] && echo 0 | | Echo 1
Final Result: 1
Explanation: If the [] condition is true, then echo 0 is executed, and if the condition of [] is false, then echo 1 is executed.

The judgment file used in the parentheses of Linux

Common parameters:

-Z: Determine if the file exists

Example:

[[email protected] ~]# [ -z adsfsfsdfs.txt ] && echo 0 || echo 11    # 最终结果为1 ,因为系统中没有存在ads...txt文件

-D: Determine if the directory exists

Example:

[[email protected] ~]# [ -d /var/www ] && echo 0 || echo 10    #输出结果为0,因为系统中有/var/www这个目录

-X to determine if the file has execute (x) permission

Example:

[[email protected] ~]# [ -x a.txt ] && echo 0 || echo 11
The judging variables of the use of parentheses in Linux

-N to determine if the variable value is empty

[[email protected] ~]# [ -n $kfjksdfjl ] && echo 0 || echo 10    #变量为空,即为9

-eq Judging whether variables or parameters are equal

Example:

[ 1 -eq 1 ] && echo 0 || echo 10  # 1=1 so...

-ne Not equal to

Example:

[ 1 -ne 2 ] && echo 0 || echo 10  # 1!=2

-GT greater than,-ge greater than or equal to

Example:

[[email protected] scripts]# [ 1 -gt 2 ] && echo 0 || echo 11

-lt less than,-le less than or equal to

[[email protected] scripts]# [ 1 -lt 2 ] && echo 0 || echo 10
Use of double brackets

Common parameters

=: equals

Example:

[[email protected] scripts]# [[ 1 = 2 ]] && echo 0 || echo 11

! = does not equal

Example:

[[email protected] scripts]# [[ 1 != 2 ]] && echo 0 || echo 10

And so on, there are >,<,<=,>= and other parameters, here is no longer one by one write out.

Copyright: arppinging

The use of parentheses in the shell Linux 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.