Shell basic calculation, logical operation, bit operation detailed _linux shell

Source: Internet
Author: User
Tags bitwise ibase logical operators
Provides an operational expression in the following format: $ ((expression))

$ echo $ ((5* (3+3))
30
$ result = $ (($myvar-10))

The shell provides a convenient number of conversions between:
$ echo $ ((013)) #八进制
$ echo $ ((0xa4)) #十六进制

You can also specify any system from 2 to 64 using the following format:
$ ((base#number))
echo $ ((8#377))
echo $ ((16#D8))

Another trick to make a conversion in a Shell is to use BC, an arbitrary precision operating language that most UNIX installers provide. Because it allows you to specify the output, this is a good technique when you need to export to a decimal system.
The special variables ibase and obase of BC contain the values for inputs and outputs, respectively. By default, all are set to 10. To perform a transform, you need to change one or two of these values, and then provide a number.

Copy Code code as follows:

$ Echo ' obase=16; 47 ' | Bc
2F
$ Echo ' obase=10; ibase=16; A03 ' | Bc
2563


With these basic features in view, the logical operators and logical expressions are looked at below, and the basic +–*/% is no longer to repeat.

One, logical operators

Logical Volume Label Express meaning
1. About the files and directories of the detection of logical labels!
-F Common! Detect the existence of "archives" eg:if [-f filename]
-D Common! Detect if "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 if it is a "symbolic link file"
-E Detect if "something" exists!
2. About the program's Logical volume label!
-G Detects whether the program performed by the GID has
-O Detection is owned by the program executed by the UID
-P Name pipe or FIFO that detects whether to send information between programs (frankly, this is not very understanding!) )
3. About the properties of the file detection!
-R Detect whether the property is readable
-W Detect if it is a property that can be written
-X Detect if it is an executable property
-S Detection is "not a blank file"
-U Detect if there is a "SUID" property
-G Detect if there is a "SGID" property
-K Detect if a property with "sticky bit"
4. judgements and comparisons between two files ; for example [Test file1-nt file2]
-nt The first file is a new one than the second.
-ot The first file is older than the second file.
-ef The first file is the same file as the second file (link file)
5. Logical and (and), or (or)
&& The meaning of the logical and
|| The meaning of the logical OR
Operational symbols Representative meaning
= equals apply to: integer or string comparison if in [], it can only be a string
!= Not equal to: integer or string comparison if in [], it can only be a 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 apply to: integer comparison
-ne Not equal to: integer comparison
-lt Less than applied to: integer comparison
-gt Greater than applies to: integer comparison
-le Less than or equal to: integer comparison
-ge Greater-than or equal to: integer comparison
-A Both sides set up (and) logical Expressions –a logical expressions
-O Unilateral establishment (or) logical expression –o logical expression
-Z Empty string
-N Non-empty string

second, the logical expression

Test command

How to use: Test EXPRESSION
Such as:
[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 '; Fi
Ok

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

Thin expression

[] An expression
[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 >,< needs to be preceded by an escape character that represents a string size comparison and a Acill code position. <> operator not directly supported, and logical operators | | && it needs to be represented by-a[and]–o[or]
[[]] An 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 is also a string comparison size. Inside supports logical operators: | | &&

Third, performance comparison

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

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

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

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

Without considering the compatibility of low version bash and SH, using [[]] is strong compatibility and performance is faster, you can use this operator when doing conditional operations.

Iv. bitwise operator Operators

Operator Name Example To interpret value
<< Move left Value=4>>2 4 Left 2 digits, value 16
>> Move right Value=8<<2 8 Right 2 bit, value 2
& Bitwise AND Value=8&&4 8 bitwise AND 4,value value is 0
| by bit or Value=8|4 8 bitwise OR 4,value value is 12
~ Bitwise NON Value=~8 Bitwise non-8,value value is-9
^ Per-bitwise XOR OR Value=10^3 10 bitwise XOR or 3,value value of 9

Note: For bitwise non, if "~a" then the result is-(a+1) Give a detailed example: Ask "~8" analysis because the computer usually
Use complement for symbolic operation, [[x] complement] =[x] so
Then 8 binary is 00001000 to 11110111.
The reverse code is 1001000 for the complement of 1001001 so the final result is 1001001,~8-9.
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.