Linux shell logical operators, logical expressions detailed

  

shell logical operators There are several types involved, so as long as the appropriate choice, we can solve many of our complex judgments, to achieve twice the result with half the effort.

1. Logical Operators

Logical Volume Labels Means Meaning 1. Detecting logical labels for files and directories! -f Common! Check if the "file" exists eg: if [ -f filename ] -d Commonly used! Check if the "directory" exists -b detect whether it is a "block file" -c detect whether it is a "character file" -S detect whether it is a "socket tag file" -L detect whether it is a symbolic Link's file -e to detect the existence of "something"! 2. About the logical label of the program! -G Detects whether it is owned by the program executed by GID -O detects whether it is owned by the program executed by UID -p detects whether it is the name pipe or FIFO for transferring information between programs (to be honest, this does not understand !) 3. About the attribute detection of files! -r Detects whether it is a readable attribute -w Detects whether it is a writable attribute -x Detects whether it is an executable attribute -s Detects whether it is a "non-blank file" -u Detects whether it has "SUID" Attribute -g Detects whether the attribute of "SGID" -k detects whether there is a "sticky bit" attribute. 4. Judgment and comparison between two files; for example [ test file1 -nt file2 ] -nt first The file is newer than the second file - the first file is older than the second file - the first file is the same file as the second file (the file like link) 5. The logical "and" ) "or (or)" && logical AND meaning | |              The meaning of logical OR


The arithmetic symbol represents meaning = equal to apply to: integer or string comparison if in [], can only be a string! = not equal to: Integer or string comparison if in [], can only be string < less than applied: integer comparison in [], can not use representation string > greater than applied: integer comparison in [], Cannot use representation string -eq equal to apply to: integer comparison -ne not equal to apply to: integer comparison -lt less than applied to: integer comparison -gt greater than applied: integer comparison -le less than or equal to apply to: Integer comparison -ge greater than or equal to apply to: integer comparison -a both sides are established (and) logical expression – a logical expression -o unilateral establishment (or) logical expression – o logical expression -z empty string - n Non-empty string


Secondary, logical expression



  • test command

    Usage :test EXPRESSION

    如:

    [root@localhost ~]# test 1 = 1 && echo 'ok'ok

    [root@localhost ~]# Test -d /etc/&& echo 'ok' ok

    [root@localhost ~]# test 1 -eq 1 && echo 'ok'ok


    [root@localhost ~]# if test 1 = 1 ; then echo 'ok'; fiok


    Note: All characters and logical operators use “space directly Separate, can't be connected together.



  • Simplified Expressions


  • [] Expressions

    [root@localhost ~]# [ 1 -eq 1 ] && echo 'ok' ok

    [root@localhost ~]# [ 2 < 1 ] && echo 'ok' -bash: 2: No such file Or directory

    [root@localhost ~]# [ 2 \\< 1 ] && echo 'ok'

    [root@localhost ~]# [ 2 -gt 1 -a 3 -lt 4 ] && echo 'ok'

    ok

    [root@localhost ~]# [ 2 -gt 1 && 3 -lt 4 ] && ; echo 'ok' -bash: [: missing `]'

    Note: In the [] expression, the common >, < need to add escape characters, indicating string size comparison, to acill The code position is used as a comparison. Does not directly support <> operators, as well as logical operators | |  && It needs to be represented by -a[and] –o[or]





  • [[] Expression

    [root@localhost ~]# [ 1 -eq 1 ] && echo 'ok' ok

    [root@localhost ~]$ [[ 2 < 3 ]] & & echo 'ok' ok
    [root@localhost ~]$ [[ 2 < 3 && 4 > 5 ]] && echo 'ok' ok

    Note: [[] The operator is just an extension of the [] operator. The ability to support <,> symbolic operations does not require an escape character, it also compares the size in a string. Support for logical operators inside:| |  &&



    Three, performance comparison

    The conditional expression of bash has three almost equivalent symbols and commands: test, []with[[]]. Usually, people are used to the form of if []; then. The appearance of [[]], according to ABS, is to be compatible with operators such as ><. The following is a comparison of their performance and found that [[]] is the fastest.

    $time (for m in {1..100000}; do test -d .;done;)real 0m0.658suser 0m0.558ssys 0m0.100s

    $ time (for m in {1..100000}; do [ -d . ];done;)real 0m0.609suser 0m0.524ssys 0m0.085s

    $ time (for m in {1..100000}; do [[ - d. ]];done;)real 0m0.311suser 0m0.275ssys 0m0.036s

    Without the compatibility of low version bash and sh, [[]] is compatible, and The performance is faster, you can use this operator when doing conditional operations.

  • Copyright © Windows knowledge All Rights Reserved