Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the four operations of shell

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand the four operations of shell". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the four operations of shell.

1. Operator (let marker)

+ addition

-subtraction

* multiplication

/ division

* * exponentiation

3 let "zonal 5 percent 3"

% take mode

Bash$ expr 5 3

two。 Comparator (note that a space is required between [] and the variable)

Integer comparator

-eq equals, for example: if ["$a"-eq "$b"] / / pay attention to the spaces

-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 或者就是未用""引用的字符串本身,放到[]结构中(见Example 7-6)虽然一般情况下可 以工作,但这是不安全的.习惯于使用""来测试字符串是一种好习惯. 3.eg 代码如下: #./bin/bash #获取英文月份对应的数字月份 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 } # 判断是否有过期短信,有调用C删除短信函数 del_overtime_sms() { num=`ipcs -q|grep "$1"|awk '{print $6}'` if (( "$num" >

"1")); then

Ipcs-Q | grep "$1" | awk'{print $2 recording 6}'> overtimesms.ini

Result of calling C function in. / del_overtime_sms / / shell

Else

Echo "there are no expired SMS messages in message queue"

Fi

}

# get the number of days corresponding to the current month

Cd / mas/shell/zhangweiatest/clear_overtime_sms

Mday= `. / getmonthofday`

Echo "days corresponding to the month before the current month = $mday"

# obtain the year and month corresponding to the system time

Buff= `date | awk'{print $2je 3}'`

Now_month=$ {buff:0:1}

Now_day=$ {buff:3:1}

# get the survival time of the current queue

Ipcs-qt | awk'{print $1pm 3 print 4}'> msglivelytime.ini

Sed-n'4 get the characters from the beginning of the fourth line to the end of the line by using msglivelytime1.ini 'msglivelytime.ini > msglivelytime1.ini

Mv msglivelytime1.ini msglivelytime.ini

While read msgid msg_month msg_day

Do

Echo

Echo "time when message queuing ID=$msgid was created: $msg_ month $msg_ dayday"

Echo "system time is $now_ daylight of $now_ month"

Getmonthofenglish "$msg_month"

Let "new_month = $msg_month + 1"

If ["$msg_month" = "Not"]; then / / is empty 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 "消息队列不安全" 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 "消息队列不安全" 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 在 shell 中的四则运算必须使用 expr 这个指令来辅助。因为这是一个指令,所以如果要将结果指定给变量,必须使用 ` 包起来。请注意,在 + - * / 的二边都有空白,如果没有空白将产生错误: 代码如下: $ expr 5 -2 3 $ sum=`expr 5 + 10` $ echo $sum 15 $ sum=`expr $sum / 3` $ echo $sum 5 还有一个要特别注意的是乘号 * 在用 expr 运算时,不可只写 *。因为 * 有其它意义,所以要使用 \* 来代表。另外,也可以用 % 来求余数。 代码如下: $ count=`expr 5 \* 3` $ echo $count $ echo `expr $count % 3` 5 我们再列出更多使用 expr 指令的方式,下列表中为可以放在指令 expr 之后的表达式。有的符号有特殊意义,必须以 \ 将它的特殊意义去除,例如 \*,否则必须用单引号将它括起来,如 '*': 类别语法说明条件判断expr1 \| expr2如果 expr1 不是零或 null 则传回 expr1,否则传回 expr2。expr1 \& expr2如果 expr1 及 expr2 都不为零或 null,则传回 expr1,否则传回 0。四则运算expr1 + expr2传回 expr1 加 expr2 后的值。expr1 - expr2传回 expr1 减 expr2 后的值。expr1\* expr2传回 expr1 乘 expr2 后的值。expr1 / expr2传回 expr1 除 expr2 后的值。expr1 % expr2传回 expr1 除 expr2 的余数。大小判断expr1 \>

Expr2 returns 1 if expr1 is greater than expr2, otherwise 0. If both expr1 and expr2 are numbers, they are judged by the size of numbers, otherwise they are judged by words. The following are all the same. Expr1\

< expr2如果 expr1 小于 expr2 则传回 1,否则传回 0。expr1 = expr2如果 expr1 等于 expr2 则传回 1,否则传回 0。expr1 != expr2如果 expr1 不等于 expr2 则传回 1,否则传回 0。expr1 \>

= expr2 returns 1 if expr1 is greater than or equal to expr2, otherwise 0. Expr1\

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report