Shell arithmetic and comparison _linux shell

Source: Internet
Author: User
Tags arithmetic echo message message queue null null

1. Operator (let marker)

+ Addition
-Subtraction
* Multiplication
/Division
* * Power operation
3 Let "z=5**3"
% modulo
bash$ Expr 5% 3

2. The comparison character (note [] and the variable need Space "")

Integral type comparison character
-eq equals, such as: if ["$a"-eq "$b"]//Note space
-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 (see example 7-6) Although it is generally available
To work, but it's not safe. It is a good habit to use "" to test strings.

3.eg

Copy Code code as follows:

#./bin/bash
#获取英文月份对应的数字月份
Getmonthofenglish ()
{
Case "$" in
"Msg_month=1");
"Feb") msg_month=2;;
"Mar") msg_month=3;;
"APR") msg_month=4;;
"may") msg_month=5;;
"June") msg_month=6;;
"June") msg_month=7;;
"Aug") msg_month=8;;
"Sept") msg_month=9;;
"Oct") msg_month=10;;
"Nov") msg_month=11;;
"Dec") msg_month=12;;
Esac
}
# to determine whether there are expired SMS, there are calls to delete the SMS function
Del_overtime_sms ()
{
Num= ' ipcs-q|grep ' $ |awk ' {print $} '
if (("$num" > "1")); Then
Ipcs-q|grep "$" |awk ' {print $2,$6} ' >overtimesms.ini
In./del_overtime_sms//shell The result of calling the C function
Else
echo "No expired SMS in Message queue"
Fi
}
#获取当前月份对应的天数
Cd/mas/shell/zhangweiatest/clear_overtime_sms
Mday= './getmonthofday '
echo "The number of days before the one months of the current month = $mday"
#获取系统时间对应的年, Month
Buff= ' Date|awk ' {print $2,$3} '
Now_month=${buff:0:1}
Now_day=${buff:3:1}
#获取当前队列的存活时间
Ipcs-qt|awk ' {print $1,$3,$4} ' >msglivelytime.ini
Sed-n ' 4, $p ' Msglivelytime.ini >msglivelytime1.ini//Get characters starting from line fourth to end
MV Msglivelytime1.ini Msglivelytime.ini
While read MsgId Msg_month msg_day
Todo
Echo
echo Message Queuing id= $msgid created at: $msg _month month $msg_day Day "
echo "System time is $now_month month $now_day Day"
Getmonthofenglish "$msg _month"
Let "New_month = $msg _month + 1"
If ["$msg _month" = "not"]; then//IS null NULL
echo "Queue Security"
elif ["$msg _month" = "]; then//blank line
echo "NULL"
elif ["$msg _month" = "$now _month"]; Then
Let "msg_day1 = $msg _day + 2"
if ("$now _day" >= "$msg _day1")); Then
if ("$msg _day" < "$mday")); Then
echo "Message Queuing is not secure"
Del_overtime_sms "$msgid"
Fi
Fi
elif ["$now _month" = "$new _month"]; Then
Let "now_day1 = $now _day + $mday"
Let "msg_day1 = $msg _day + 2"
echo "now_day1= $now _day1,msg_day1= $msg _day1"
if ("$now _day1" >= "$msg _day1")); Then
# if ("$msg _day1" < "$mday")); Then
echo "Message Queuing is not secure"
Del_overtime_sms "$msgid"
# fi
Fi
Elif ("$now _month" > "$new _month")); Then
echo "" $msg _month > "$now _month" "
Del_overtime_sms "$msgid"
echo "Message Queuing is not secure"
Else
echo "MSG Queue safe"
Fi
Done < Msglivelytime.ini


The arithmetic in the shell must be assisted by using the instruction expr. Because this is an instruction, you must use ' wrap ' if you want to assign the result to a variable. Note that there is a white space on the +-*/Two side, and an error will occur if there is no white space:

Copy Code code as follows:

$ expr 5-2
3
$ Sum= ' Expr 5 + 10 '
$ echo $sum
15
$ Sum= ' Expr $sum/3 '
$ echo $sum
5


Another particular note is that multiplication * is not written in the expr operation. Because * has other meanings, so use \* to represent. In addition, you can also use% to find the remainder.

Copy Code code as follows:

$ Count= ' Expr 5 \* 3 '
$ echo $count
$ echo ' Expr $count% 3 '
5


We'll list more ways to use the expr directive, and the following list is an expression that can be placed after the instruction expr. Some symbols have special meanings that must be removed by the special meaning of \, such as \*, or they must be enclosed in single quotes, such as ' * ':

Category Grammar Description
Conditional judgment Expr1 \| Expr2 If EXPR1 is not 0 or null, return EXPR1, or return EXPR2.
Expr1 \& EXPR2 If both Expr1 and EXPR2 are Non-zero or null, then the EXPR1 is returned, or 0 is returned.
Arithmetic Expr1 + EXPR2 Returns the value after Expr1 plus expr2.
Expr1-expr2 Returns the value after Expr1 minus expr2.
expr1\* EXPR2 Returns the value after the Expr1 multiplication expr2.
Expr1/expr2 Returns the value after Expr1 except EXPR2.
Expr1% EXPR2 Returns the remainder of the EXPR1 except EXPR2.
Size judgment Expr1 \> EXPR2 If EXPR1 is greater than EXPR2, it returns 1, otherwise it returns 0. If Expr1 and expr2 are numbers, they are judged by the size of the figures, otherwise they are judged by words. The following are all the same.
Expr1 \< EXPR2 If EXPR1 is less than EXPR2, it returns 1, otherwise it returns 0.
EXPR1 = Expr2 If Expr1 equals EXPR2, it returns 1, otherwise it returns 0.
Expr1!= EXPR2 If EXPR1 is not equal to EXPR2, it returns 1, otherwise it returns 0.
Expr1 \>= EXPR2 If EXPR1 is greater than or equal to EXPR2, it returns 1, otherwise it returns 0.
Expr1 \<= EXPR2 If the expr1 is less than or equal to EXPR2, it returns 1, otherwise it returns 0.
word processing Expr1:expr2 Compares a fixed string, that is, regular expression. You can use the following characters to assist:

. Matches one character.

$ to find the end of the string.

[List] Find any strings that match the list.

* Search for 0 or more words before *.

\ (\) Returns a string that matches in parentheses.


We will give examples of more complex word processing parts:

Copy Code code as follows:

$ tty
Ttyp0
$ Expr ' tty ': '. *\ (. \)\$"
P0
$ Expr ' tty ': '. *\ (... \)$'
P0

The result of the above TTY is ttyp0, and in expr, in the expression on the right, look first. * Represents 0 or more of any characters, returned after the end ($) of the two characters (... \)。 In the first expr formula, because of the use of double quotes, we use a \ To remove the special meaning of $ before $, while the second expr uses single quotes and the words in single quotes lose their special meaning, so there is no need to add \ before $.

In addition to using expr, we can use the following special syntax:

Copy Code code as follows:

$ a=10
$ b=5
$ c=$ ((${a}+${b}))
$ echo $c
15
$ c=$ ((${a}*${b}))
$ echo $c
50

We can use $ (()) to put an expression in parentheses to achieve the function of the operation.

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.