Introduction to comparison symbols and special symbols in Linux shell _linux Shell

Source: Internet
Author: User
Tags regular expression
Shell string comparisons, judging whether it's a number

Binary comparison operators, comparing variables or comparing numbers. Notice the difference between the number and the string.

Integer Comparisons

-eq equals, such as: if ["$a"-eq "$b"]
-ne is not equal to, such as: if ["$a"-ne "$b"]
-GT is greater than, such as: if ["$a"-gt "$b"]
-ge is greater than or equal to, such as: if ["$a"-ge "$b"]
-lt is less than, such as: if ["$a"-lt "$b"]
-le is less than or equal, such as: if ["$a"-le "$b"]
< less than (requires double brackets), such as: (("$a" < "$b")
<= is less than or equal (requires double parenthesis), such as: (("$a" <= "$b")
> Greater than (requires double brackets), such as: (("$a" > "$b")
>= is greater than or equal (requires double parenthesis), such as: (("$a" >= "$b")

string Comparisons

= equals, such as: if ["$a" = "$b"]
= = equals, such as: if ["$a" = = "$b"], and = equivalent
Note: the function of = = = is different in [[]] and [], as follows:

1 [[$a = = z*]] # If $a starts with "Z" (pattern match) then it will be true
2 [[$a = = "z*"]] # if $a equals z* (character match), then the result is true
3
4 [$a = = z*] # File globbing and word splitting will occur
5 ["$a" = "z*"] # if $a equals z* (character match), then the result is true

A little explanation, about the file globbing is a shorthand for documents, such as "*.c" is, and so on.
But file globbing is not a strict regular expression, although in most cases the structure is more like.

!= is not equal to, such as: if ["$a"!= "$b"]
This operator will use pattern matching in the [[]] structure.
< less than, in ASCII alphabetical order. For example:
if [["$a" < "$b"]]
If ["$a" \< "$b"]
Note: In the [] structure, "<" needs to be escaped.
> Greater than, in ASCII alphabetical order. For example:
if [["$a" > "$b"]]
If ["$a" \> "$b"]
Note: In the [] structure, ">" needs to be escaped.
Refer to Example 26-11 to see examples of this operator application.
The-Z string is "null". That is, the length is 0.
-N string is not "null"

Attention:

Using-n tests in the [] structure must be caused by the variable "". Use a string that is not ""! -Z
Or the string itself, which is not quoted, is placed in the [] structure. Although in general you can
To work, but it's not safe. It is a good habit to use "" to test strings.

awk ' {print $} ' class.txt | grep ' ^[0-9.] ' > Res

the numerical comparison and calculation under shell

Comparison:

Method One: If [${a}-lt ${b}]; Then ...
This is the most basic comparison method, using LT (less than), GT (greater than), LE (less than equal), GE (greater than equal), advantages: not found; disadvantage: can only compare integers, use lt,gt and so on not intuitive

Method Two: if ((${a} < ${b})) then ...
This is the Cshell style comparison, advantages: Do not use LT,GT and other difficult to remember the string; disadvantage: or only to compare integers

Method Three: if (Echo ${a} ${b} | awk '! $1>$2) {Exit 1} ') then ...
This is the advantage of using awk: you can compare decimals; disadvantages: Expressions are too complex to remember

Method IV: if (echo ${a}-${b} | bc-q | grep-q "^-"); Then ...
This is calculated using the BC comparison, the advantages: you can compare decimals; disadvantages: more complex expressions, difficult to remember

Calculation:
Method One: typeset c=$ (expr ${a} + ${b});
The basic tools in the shell, advantages: convenient to detect whether the variable is a number; disadvantages: only integers can be computed, and only addition and subtraction can be computed, and the multiplication and division method cannot be computed.

Method Two: Let "c=${a}+${b}"; or let "c=a+b"
Built-in command calculation, advantages: can calculate multiplication and division and bitwise operation; disadvantages: only integers can be computed

Method III: Typeset c=$ ((a+b))
Cshell style calculation, advantages: can calculate multiplication and division method and bit operation, introduction, easy to write; disadvantage: cannot calculate decimal

Method Four: typeset C=${echo ${a} ${b} | awk ' {print $1+$2} ')
The advantage of using awk calculation is that it is possible to compute decimal numbers, to achieve a variety of calculations, and to compute flexibly; Disadvantages: too complex an expression

Method Five: typeset c=${echo ${a} + ${b} | bc-q)
The advantage of using awk calculation is that you can calculate decimals, compute more than awk, and compute flexibly; disadvantage: the expression is too complex, the number of digits after the decimal point must be set using Scale=n, or the result may be truncated to an integer


Special Characters

Symbol use
General situation we output a command to press a carriage return, if you want to execute multiple commands on one line, the middle can be divided into cd/home; Ls
* denotes any character (regular)
? Any one character
One of the [ABC] list items
[^ABC] You can also use ranges for list fetching [A-z] [0-9] [A-z] (all characters and numbers)
{} When you use touch_{1,2,3} in the loop list, Touch_1,touch_2,touch_3 loops out the three files, and the Echo ${ab}c
~ Home Directory CD ~ (ordinary call into the/home directory under the user's own family directory)
$ extract Variable Value
' $ () command replaces touch ' date +%f_\ ' date +%t\ ' touch $ (date +%f_$ (date +%t))
$[] integer computes echo $[2+3]-*/% floating-point number with echo ' scale=3; 10/3 "| Bc-l (BC for calculation)
\ escape string echo \ \ output \ Escape special characters to prevent Shell from interpreting special characters in bash
"" "with a space string to treat a space as part of a string echo" ABC xyz "ECHO ' ABC xyz '
"Command in exchange for the execution of the order.
$ () ditto, but it compensates for the ' nesting flaw
@ No special meaning
# Comments (General programming needs to be annotated to allow other team members to understand the program features they write)
$ variable Take value
$ () command substitution
${} variable name range
% kill the background often jobs, modulo operation (we should take the model is not unfamiliar)
^ Pick-and-do! Identical
& Process Background processing, && for logic and
* match any string; compute multiplication
() Child Process Execution
-Minus, range, CD-Go back to top directory, kill current jobs

_ (underline) no special meaning
+ Plus; Kill current jobs (process)
= Assign Value
| Pipeline, | | Logical OR
\ Escape When some special symbol such as $ is a variable that needs to be escaped to not be resolved by bash
{} Command list {ls;cd/;}
[] character wildcard, [] is also used to test commands
: Empty Command truth
; command Terminator
"Soft primer" hard lead
< input redirection
> Output redirection
>& Merge 2 and 1 outputs
, enumeration separator
. Current directory
/directory Separator
? Single character
Carriage return Command execution
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.