Original:
Http://blog.pureisle.net/archives/1352.html
The Shell provides a large number of basic operations that are useful in scripting. The Shell evaluates the arithmetic expression you provide, performs an operation expansion, and replaces an expression with the resulting result. 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.
$ Echo ' obase=16; 47 ' | BC
2F
$ Echo ' obase=10; ibase=16; A03 ' | BC
2563
With these basic features in view, see the logical operators and logical expressions below, and the basic +-*/% no longer repeat.
One, logical operators
Logical Volume Label |
Express meaning |
1. |
A logical volume label for the detection of files and directories. |
-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 logical volume label for the program. |
-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 (honestly, this is not quite understood.) ) |
3. |
About the property detection of the file. |
-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 |
operator |
represents meaning |
= |
equals apply to: integer or string comparisons if in [], only characters String |
!= |
is not equal to apply to: integer or string comparisons if in [], only strings |
|
less than applied to: integer comparison in [], cannot use the representation string |
|
is greater than applies to: integer comparison in [], cannot use representation string |
-eq |
equals apply to: integer comparison |
-ne |
is not equal to apply to: integer comparison |
-lt |
is less than applied to: integer comparison |
-gt |
is greater than applies to: integer comparison |
-le |
is less than or equal to apply to: integer comparison |
-ge |
is greater than or equal to: integer comparison |
-A |
both set up (and) logical Expressions –a logical Expressions |
-O |
unilaterally set (or) logical expression –o logical expression |
-Z |
empty string |
-N |
non-empty string |
Second, the logical expression
Test command