In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what is the difference between comparison symbols and special symbols in linux shell, it has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Compare shell strings and judge whether they are numeric or not
Binary comparison operators, comparison variables, or comparison numbers. Notice the difference between a number and a string.
Integer comparison
-eq equals, for example: if ["$a"-eq "$b"]
-ne is not equal to, for example: if ["$a"-ne "$b"]
-gt is greater than, for example: if ["$a"-gt "$b"]
-ge is greater than or equal to, such as: if ["$a"-ge "$b"]
-lt is less than, for example: if ["$a"-lt "$b"]
-le is less than or equal to, such as: if ["$a"-le "$b"]
< 小于(需要双括号),如:(("$a" < "$b")) "$b")) >= greater than or equal (double parentheses are required), such as: (("$a" > = "$b"))
String comparison
= equal to, for example: if ["$a" = "$b"]
= equal to, for example: if ["$a" = = "$b"], equivalent to =
Note: the function of = = behaves differently in [[]] and [], as follows:
1 [[$a = = z*]] # if $a starts with "z" (pattern match) then it will be true
2 [[$a = "z*"] # if $an equals z * (character match), then the result is true
three
4 [$a = = z*] # File globbing and word splitting will happen
5 ["$a" = "z*"] # if $an equals z * (character match), then the result is true
A little explanation, about File globbing is a kind of shorthand about files, such as "* .c" is, and so is also.
But file globbing is not a strict regular expression, although in most cases the structure is similar.
! = not equal to, for example: if ["$a"! = "$b"]
This operator will use pattern matching in the [[]] structure.
< 小于,在ASCII字母顺序下.如: if [[ "$a" < "$b" ]] if [ "$a" \< "$b" ] 注意:在[]结构中""需要被转义. 具体参考Example 26-11来查看这个操作符应用的例子. -z 字符串为"null".就是长度为0. -n 字符串不为"null" 注意: 使用-n在[]结构中测试必须要用""把变量引起来.使用一个未被""的字符串来使用! -z 或者就是未用""引用的字符串本身,放到[]结构中。虽然一般情况下可 以工作,但这是不安全的.习惯于使用""来测试字符串是一种好习惯. awk '{print $2}' class.txt | grep '^[0-9.]' >Res
Numerical comparison and calculation under SHELL
Compare:
Method one: if [${A}-lt ${B}]; then.
This is the most basic comparison method, using lt (less than), gt (greater than), le (less than or equal to), ge (greater than or equal to), advantages: not found; disadvantages: can only compare integers, the use of lt,gt is not intuitive
Method 2: if (${A})
< ${B})) then ... 这是CShell风格比较,优点:不用使用lt,gt等难记的字符串;缺点:还是只能比较整数 方法三: if (echo ${A} ${B} | awk '!($1>$2) {exit 1}') then...
This is the use of awk comparison, advantages: can compare decimals; disadvantages: expressions are too complex to remember
Method 4: if (echo ${A}-${B} | bc-Q | grep-Q "^ -"); then.
This is to use bc to calculate the comparison, advantages: you can compare decimals; disadvantages: expressions are more complex and difficult to remember
Calculate:
Method 1: typeset paid $(expr ${A} + ${B})
SHELL basic tools, advantages: easy to detect whether variables are numeric; disadvantages: can only calculate integers, and can only calculate addition and subtraction, can not calculate multiplication and division
Method 2: let "paid ${A} + ${B}"; or let "C=A+B"
Embedded command calculation, advantages: can calculate multiplication and division and bit operations, etc.; disadvantages: can only calculate integers
Method 3: typeset paid $((Atom B))
CShell style calculation, advantages: can calculate multiplication and division and bit operations, brief introduction, easy to compile; disadvantages: can not calculate decimals
Method 4: typeset Clearing ${echo ${A} ${B} | awk'{print $1clients 2}')
Using awk calculation, advantages: can calculate decimals, can achieve a variety of calculation methods, flexible calculation; disadvantages: expressions are too complex
Method 5: typeset paid ${echo ${A} + ${B} | bc-Q)
Using awk calculation, the advantages: can calculate decimals, the calculation method is more than awk, the calculation is flexible; disadvantages: the expression is too complex, the number of digits after the decimal point must be set using scale=N, otherwise the result may be truncated to integer
Special character
Symbol usage
In general, after we output a command, we need to press enter. If you want to execute multiple commands on one line, you can use the; sign to split cd / home; ls
* represents any character (regular)
? Any character
[abc] one of the list items
[^ abc] you can also use the range [Amurz] [0-9] [Amurz] for list denotation (all characters and numbers)
{} when you loop the list with touch_ {1 ab 2 and 3}, you will set up a touch_1,touch_2,touch_3 to loop out these three files, and you will also use echo ${ab} c.
~ home directory cd ~ (ordinary calls enter the user's own home directory under the / home directory)
$extract variable value
The ``$ () command replaces touch `date +% F _ `date +% T\ ``touch $(date +% fame $(date +% T))
$[] Integer calculation echo $[2p3]-* /% floating point number using echo "scale=3; 10tick 3" | bc-l (used by bc to calculate)
\ escape string echo\\ output\ escape special characters to prevent special characters in bash from being interpreted by SHELL
To treat spaces as part of a string echo "abc xyz" echo 'abc xyz'
The ``command replaces the execution result of the command
$() is the same as above, but it makes up for the nesting defect of ``
@ has no special meaning
# comments (general programming requires annotations to let other team members know the functions of the programs they write)
Value of $variable
$() command replacement
Range of ${} variable names
Kill the jobs number often in the background, take the module operation (you should be no stranger to the model)
^ take the non-peace! Identical
& with process background processing, & & for logic and
* match any string; calculate multiplication
() child process execution
-minus sign, interval, cd-go back to the upper directory and kill the current jobs
_ (underscore) has no special meaning
+ plus sign; kill current jobs (process)
= assignment
| | Pipeline, | | Logic or |
Escape when some special symbol, such as $, is a variable that needs to be escaped so that it is not parsed by bash
{} Command list {ls;cd /;}
[] character wildcard, [] is also used for test commands
Empty command true value
; command Terminator
"" soft citation "hard citation"
< 输入重定向 >Output redirection
> & merge 2 and 1 output
Enumerating delimiters
. Current directory
/ directory separator
? Single character
Enter command execution
Thank you for reading this article carefully. I hope the article "what is the difference between comparative symbols and special symbols in linux shell" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.