Shell's If statement

Source: Internet
Author: User
Tags bitwise logical operators




First, the logical operator

Logical Volume Label Express meaning
1. About the file and directory of the detection of Logical volume label!
-F Common! Detect if "Archive" exists eg:if [-f filename]
-D Common! Detects if the directory exists
-B Detects if it is a "block file"
-C Detects if it is a "character file"
-S Detects if it is a "socket tag file"
-L Detects if it is a "symbolic Link's profile"
-E Detect if "something" exists!
2. About the logical volume label of the program!
-G Detect whether the program owned by the GID
-O Detection is owned by the program executed by the UID
-P Detects if a name pipe or FIFO is being transmitted between programs (to be honest, this is not a good idea!). )
3. About the property detection of the file!
-R Detect if a property is readable
-W Detects if a property can be written
-X Detect whether the property is executable
-S Detects if it is a "non-blank file"
-U Detect if a property with "SUID" is
-G Detect if a property with "SGID" is
-K Detect if a property with "sticky bit" is
4. judgments and comparisons between two files ; for example [Test file1-nt file2]
-nt The first file is newer than the second one.
-ot The first file is older than the second one.
-ef The first file is the same file as the second file (link and other files)
5. Logical AND (and) "or (OR)"
&& The meaning of the logical and
|| The meaning of a logical OR


Operation symbols Representative meaning
= Equal to: Integer or string comparison if in [], only the string
!= Not equal to: integer or string comparison if in [], only the string
< Less than applied: integer comparison in [], cannot use the representation string
> Greater than applied: integer comparison in [], cannot use the representation string
-eq equals applies To: integer comparison
-ne Not equal to: integer comparison
-lt Less than applied to: integer comparison
-gt Greater than applied to: integer comparison
-le Less than or equal to: integer comparison
-ge Greater than or equal to: integer comparison
-A Both sides established (and) logical Expressions –a logical expressions
-O Single-sided (or) logical expression –o logical expression
-Z Empty string
-N Non-empty string


Ii. Logical Expressions

Test command

How to use: Test EXPRESSION
Such as:
[[email protected] ~]# Test 1 = 1 && echo ' OK '
Ok
[[email protected] ~]# test-d/etc/&& echo ' OK '
Ok
[[email protected] ~]# Test 1-eq 1 && echo ' OK '
Ok
[[Email protected] ~]# if test 1 = 1; Then echo ' OK '; Fi
Ok

Note: All characters are separated from the logical operators directly with "spaces" and cannot be joined together.

Thin expressions

[] Expression
[[Email protected] ~]# [1-eq 1] && echo ' OK '
Ok
[[Email protected] ~]# [2 < 1] && echo ' OK '
-bash:2: No such file or directory
[[Email protected] ~]# [2 \< 1] && echo ' OK '
[[Email protected] ~]# [2-GT 1-a 3-lt 4] && echo ' OK '
Ok
[[Email protected] ~]# [2-GT 1 && 3-lt 4] && echo ' OK '
-bash: [: Missing '] '
Note: In the [] expression, the common >,< need to add an escape character, which represents the string size comparison, as compared to the Acill code position. Do not directly support the <> operator, there are logical operators | | && it needs to be represented by-a[and]–o[or]
[[]] expressions
[[Email protected] ~]# [1-eq 1] && echo ' OK '
Ok[[email protected] ~]$ [[2 < 3]] && echo ' OK '
Ok
[[Email protected] ~]$ [[2 < 3 && 4 > 5]] && echo ' OK '
Ok

Note: the [[]] operator is only an extension of the [] operator. The ability to support the <,> symbol operation does not require an escape character, it is also a string comparison size. Logical operators are supported inside: | | &&

Third, performance comparison

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

$ time (for M in {1..100000}; do test-d.; Done;)
Real 0m0.658s
User 0m0.558s
SYS 0m0.100s

$ time (for M in {1..100000}; do [-D.]; Done;)
Real 0m0.609s
User 0m0.524s
SYS 0m0.085s

$ time (for M in {1..100000}; do [[-D.]]; Done;)
Real 0m0.311s
User 0m0.275s
SYS 0m0.036s

Regardless of the low version of Bash and the compatibility of SH, with [[]] is strong compatibility, and performance is relatively fast, in the case of a conditional operation, you can use the operator.


Four, bitwise operator

Operator Name Example Interpret value
<< Move left Value=4>>2 4 Shift left 2 bits, value 16
>> Move right Value=8<<2 8 Move right 2 bit, value is 2
& Bitwise-AND Value=8&&4 8 Bitwise AND 4,value values of 0
| Bitwise OR Value=8|4 8 bitwise OR 4,value value of 12
~ Bitwise non- Value=~8 Bitwise non-8,value value is-9
^ Bitwise XOR OR Value=10^3 10 bitwise XOR or 3,value value is 9


Note: For bitwise non, if the "~a" result is-(a+1) to give a detailed example: "To" analysis because the computer is usually
Sign operation with complement, [[[x] complement] complement =[x] so
Then 8 of the binary is 00001000 for non-11110111
The negation code is 1001000 for the complement of 1001001 so the final result is 1001001,~8-9.



Note: spaces are required within [] in the IF statement

If [$num-eq 100];then #错误

If [$num-eq100];then #错误

If [$num-eq100];then #错误

If [$num-eq];then #正确


If single Branch

[email protected] ~]$ cat test3.sh
#!/bin/bash

Read-p "Enter your score:" num
If [$num-eq];then
echo "Congratulations on your perfect score."
Fi
[Email protected] ~]$

[Email protected] ~]$./test3.sh
Enter your score: 100
Congratulations on getting full marks.
[Email protected] ~]$


If dual Branch

[email protected] ~]$ cat test4.sh
#!/bin/bash

Read-p "Enter your score:" num
If [$num-ge];then
Echo, "you passed."
Else
Echo, "You failed."
Fi
[Email protected] ~]$
[Email protected] ~]$./test4.sh
Enter your score: 70
You passed the exam.
[Email protected] ~]$./test4.sh
Enter your score: 59
You didn't pass.
[Email protected] ~]$


If multi-branch

[email protected] ~]$ cat test5.sh
#!/bin/bash

Read-p "Enter your score:" num
If [$num-ge 60-a $num-lt];then
Echo, "you passed."
elif [$num-eq];then
echo "Congratulations on your perfect score."
elif [$num-lt 60-a $num-ge 0];then
Echo, "You failed."
Else
echo "Please re-enter"
Fi
[Email protected] ~]$

[Email protected] ~]$./test5.sh
Enter your score: 100
Congratulations on getting full marks.
[Email protected] ~]$./test5.sh
Enter your score: 99
You passed the exam.
[Email protected] ~]$./test5.sh
Enter your score: 59
You didn't pass.
[Email protected] ~]$./test5.sh
Enter your score:-1
Please re-enter


This article is from the "Xiao Mo" blog, please be sure to keep this source http://xiaofengmo.blog.51cto.com/10116365/1744961

Shell's If statement

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.