Shell string comparison, judging whether it is a numeric two-dollar comparison operator, compare variables or compare numbers. Note the difference between a number and a string. Integer comparison -eq equals, such as:if [ "$a" -eq "$b" ] -ne not equal to, such as: if [ $a -ne $b ] -gt greater than, such as:if [ "$a" -gt "$b" ] -ge greater than equals, such as:if [ "$a" -ge "$b" ] -lt less than, such as:if [ "$a" -lt "$b" ] -le less than equals, such as:if [ "$a" -le "$b" ] < less than (requires double brackets), such as: (("$a" < "$b") <= less than equals (requires double brackets), such as: (("$a" <= "$b") > greater than ( Double brackets are required), such as: (("$a" > "$b") >= greater than equals (requires double brackets), such as: (("$a" >= "$b") string comparison = equals, such as:if [ "$a" = "$b" ] == equals, such as:if [ "$a" == "$b" &NBSP;], 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 is equal to z* (character match), then the result is true a little explanation, about file globbing is a shorthand for the document, such as "*.c" is, again like . but file Globbing is not a strict regular expression, although in most cases the structure is less like . != , such as:if [ "$a" != "$b" ] This operator will use pattern matching in the [[]] structure . < less than, in ASCII alphabetical order. such as: if [[ "$a" < "$b" &NBSP;] if [ "$a" \< "$b" ] Note: in [] structure "<" needs to be escaped . > greater than, In ASCII alphabetical order. such as: if [[ "$a" > "$b" ]] if [ "$a" \> "$b" ] Note: in [] structure ">" needs to be escaped . specific reference example 26-11 to see an example of this operator's application . -z string "null". That is, the length is 0. -n string is not "null" note: using-n in the [] structure must be used "" to cause the variable to be used. Use a string that is not "" with! -z or just unused "" The referenced string itself, placed in the [] structure. Although it is generally possible to work, it is unsafe. Accustomed to using "" toTesting strings is a good habit . awk ' {print $2} ' class.txt | grep ' ^[0-9. ' > res shell Comparison of numbers and calculation 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 equals), GE (greater than equals), advantages: not found; disadvantage: can only compare integers, using LT,GT and other non-intuitive method two: if ((${a} < ${b})) then ... This is Cshell style comparison, Pros: Don't use LT,GT and other hard-to-remember strings ; disadvantage: Still can only compare integer method three: if (echo ${a} ${b} | awk '! $1>$2) {exit 1} ') then ... this is the advantage of using awk comparison: you can compare decimals; disadvantage: the expression is too complex, difficult to remember method four: if (Echo ${A} - ${B} | bc -q | grep -q "^-"); then This is a comparison using BC calculations, advantages: you can compare decimals; Cons: more complex expressions, difficult to remember calculate: method One: typeset c=$ (expr ${a} + ${b}); shell Basic Tools, advantages: Convenient to detect whether the variable is a number; disadvantage: only integers can be computed, and only add and subtract can be computed, multiplication method method two:let "C=${a}+${b}"; or let is not calculated "c=a+b" embedded command calculation, advantages: Can calculate multiplication method and bit arithmetic, etc. disadvantage: Only integer method three:typeset c=$ ((a+b)) cshell style calculation, advantages: Can calculate multiplication method and bit operation, introduction, easy to write; disadvantage: cannot calculate decimal method four: Typeset c=${echo ${a} ${b } | awk ' {print $1+$2} ') using awk calculation, the advantage: can calculate the decimal, can achieve a variety of calculation methods, computational flexibility; disadvantage: the expression is too complex method five: typeset &NBSP;C=${ECHO&NBSP;${A}&NBSP;+&NBSP;${B}&NBSP;|&NBSP;BC&NBSP;-Q) The advantage of using AWK calculation: the ability to calculate decimals, more computing than awk, flexible computing ; Disadvantage: 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 integers special characters symbols 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 used; number split cd /home ; ls * denotes any character (regular) ? any character [abc] One of the list items [^abc] for list take non can also use range [a-z] [0-9] [a-z] (all characters and numbers) {} loop list touch_{1,2,3} will be established Touch_1,touch_2,touch_3 loop out the three files, will also use the echo ${ab}c ~ home directory cd ~ (normal calls into the home directory of the user's own directory) $ extract variable values ' $ () command replace touch ' date +%f_\ ' date +%t\ ' touch $ (date +%f_$ (date +%t)) $[] integer calculation echo $[2+3] - * / %&nbSP; floating point echo "SCALE=3;&NBSP;10/3" | bc -l (BC for calculation) \ escaping the following string echo \\ output \ Escape special characters to prevent the shell from interpreting special characters in bash " " with a space string spaces as part of a string echo "abc xyz" echo ' abc xyz ' ' command replacement command execution Results $ () Ibid, but it makes up ' Nested defects @ No special meanings # annotations (general programming needs to be annotated to let other team members know about their own program functions) $ variable Value $ () command substitution ${} variable name range % Kill background often jobs number, modulo operation (everyone should not be unfamiliar with the model) ^ take non and ! Duplicate & process, && for logic and * match arbitrary strings; compute multiplication () sub-process execution - minus, interval ,cd - back to Upper directory, kill current jobs _ (underscore) no special meaning + plus; kill current jobs (process) = assignment | Piping, | | Logic or \ escape when some special symbols such as $ are a variable that need to be escaped before Bash Parsing {} command list {ls;cd /;} [] character wildcard, [] also used for test commands : null command Truth ; command Terminator " soft citation " Hard lead < input redirection > output redirection >& Merge 2 and 1 output , enum delimiter . current directory / directory delimiter ? single character enter command execution
This article is from the "Sunset" blog, please be sure to keep this source http://whluwit.blog.51cto.com/2306565/1438150