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
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.
[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]
[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.
For the daily use of the Linux system, it is necessary to use the command line to carry out, for ex
badblocks is the command or utility in linux like operating system which can scan or test our hard d
The missing header file ‘security/pam_modules.h’ is part of the libpam development, a P
The Linux command line is useful, efficient, and fun, but sometimes dangerous, especially if you are
CentOS PPTP VPN Server (32-bit) installation basic tutorial
Java can not handle graphics solutions under Linux
How to use the capability feature to enhance the security of Linux systems
Linux regularly performs tasks. Implementation Tutorial
Suse linux single-user mode to modify the root password
Hard disk bad track detection tutorial under Linux
A sentence sentence iptables detailed Chinese manual
The method of teaching you whether to judge whether the Linux system is hacked is
Solution for loss or damage of winload.exe after booting the computer
Three operating modes of disk partition under win8
How to close Win10 speech recognition? Win10 closes the method of speech recognition
Linux lanmp/lamp/lnmp one-click installation package
What problems should I pay attention to when cleaning Win8 computers?
Microsoft released Windows10 Build 10041 is available for download