SHELL arithmetic operations and Comparison

Source: Internet
Author: User

Arithmetic Operations and Comparison

1. Operator (let identifier)

+ Addition-subtraction * multiplication/division ** power operation 3 let "z = 5 ** 3" % modulo bash $ expr 5% 3
2. comparison operator (note that a space is required between [] and the variable "") integer comparison operator-eq is equal to, for example: if ["$ a"-eq "$ B"] // note Space
-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, for example, if ["$ a"-ge "$ B"]-lt is less than, for example: if ["$ a"-lt "$ B"]-le is less than or equal to, for example: if ["$ a"-le "$ B"] <less than (double parentheses are required), such as: ("$ a" <"$ B ")) <= less than or equal to (double parentheses required), such as: ("$ a" <= "$ B")> greater than (double parentheses required ), for example: ("$ a"> "$ B")> = equal to or greater than (double parentheses are required), for example: ("$ a"> = "$ B ")) string comparison = equals, such as: if ["$ a" = "$ B"] = equals, such as: if ["$ a" = "$ B"], equivalent to = Note: The = function has different behaviors in [[] and [], as shown below: 1 [[$ a = z *] # If $ a starts with "z" (pattern matching) True 2 [[[$ a = "z *"] # If $ a is equal to z * (character matching ), the result is true 3 4 [$ a = z *] # File globbing and word splitting will occur 5 ["$ a" = "z *"] # If $ equal to z * (character matching ), the result is true. File globbing is a stenographer about a File, for example "*. c "is, like ~ Yes. But file globbing is not a strict regular expression. Although the structure is similar in most cases .! = Not equal to, such as: if ["$ "! = "$ B"] This operator uses pattern matching in the [[] structure. <less than, in the ASCII alphabetic order. for example, if [["$ a" <"$ B"] if ["$ a" \ <"$ B"] Note: in the [] structure, "<" needs to be escaped.> greater than, in the ASCII alphabetic order. for example, if [["$ a"> "$ B"] if ["$ a" \> "$ B"] Note: in the [] structure, ">" must be escaped. for more information, see Example 26-11. -The z string is "null ". the length is 0. -n string is not "null" Note: To use-n to test in the [] structure, you must use "" to cause the variable. use a string that is not! -Z or a string that is not referenced by "", put it in the [] structure (see Example 7-6). Although it can usually work, it is not safe. it is a good habit to use "" To test strings.

3. eg

#./Bin/bash
# Obtain the digit month corresponding to the English month
Getmonthofenglish ()
{
Case "$1" in
"Jan") msg_month = 1 ;;
"Feb") msg_month = 2 ;;
"Mar") msg_month = 3 ;;
"Apr") msg_month = 4 ;;
"May") msg_month = 5 ;;
"Jun") msg_month = 6 ;;
"Jul") msg_month = 7 ;;
"Aug") msg_month = 8 ;;
"Sept") msg_month = 9 ;;
"Oct") msg_month = 10 ;;
"Nov") msg_month = 11 ;;
"Dec") msg_month = 12 ;;
Esac
}
# Determine whether there are expired SMS messages and call C to delete the SMS function
Del_overtime_sms ()
{
Num = 'ipcs-q | grep "$1" | awk '{print $6 }''
If ("$ num"> "1"); then
Ipcs-q | grep "$1" | awk '{print $2, $6}'> overtimesms. ini
./Del_overtime_sms // result of Calling C function in shell
Else
Echo "No expired SMS messages in the message queue"
Fi
}
# Obtain the number of days for the current month
Cd/mas/shell/zhangweiatest/clear_overtime_sms
Mday = './getmonthofday'
Echo "the number of days corresponding to the previous month in the current month = $ mday"

# Obtain the year and month corresponding to the system time
Buff = 'date | awk' {print $2, $3 }''
Now_month =$ {buff: 0: 1}
Now_day =$ {buff: 3: 1}

# Obtain the survival time of the current queue
Ipcs-qt | awk '{print $1, $3, $4}'> msglivelytime. ini
Sed-n'4, $ P' msglivelytime. ini> msglivelytime1.ini // gets the characters starting from the fourth row to the end.
Mv msglivelytime1.ini msglivelytime. ini

While read msgid msg_month msg_day
Do
Echo
Echo "Message Queue ID = $ msgid created at: $ msg_month $ msg_day"
Echo "the system time is the $ now_day day of $ now_month"
Getmonthofenglish "$ msg_month"
Let "new_month = $ msg_month + 1"
If ["$ msg_month" = "Not"]; then // NULL
Echo "queue security"
Elif ["$ msg_month" = ""]; then // empty row
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 "insecure message queue"
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 "insecure message queue"
Del_overtime_sms "$ msgid"
# Fi
Fi
Elif ("$ now_month"> "$ new_month"); then
Echo "" $ msg_month ">" $ now_month ""
Del_overtime_sms "$ msgid"
Echo "insecure message queue"
Else
Echo "msg queue safe"
Fi
Done <msglivelytime. ini

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.