shell中let 命令与Expr命令介绍

let 命令介绍:

Let命令让BASH shell执行算数运算的操作,使用let,可以比较两运算数值或者执行加减乘除等运算操作,这种操作往往用于shell程序中的流程控制结构或者执行需要的运算,注意let只能执行整数的相关操作,运算结果也只能保存整数。

使用方法如下:

let 变量名 = 变量1 运算符 变量2

 

常见的算数操作分类:

加法:+

减法:-

除法:/

乘法:*

取余数:%

 

Let命令使用方法举例:

注意:和c语言类似,;let i=$i+1可以写成let i++来简化书写

Shell实例1:

[root@ChangerLee 运算比较符]#cat let_op.sh 

#!/bin/bash

#some examples about let

 

 

num1=106

num2=10

num3=5

#oprating 

let res1=${num1}+${num2}+${num3}

let res2=${num1}-${num2}-${num3}

let res3=${num1}*${num2}*${num3}

let res4=${num1}/${num2}/${num3}

let res5=${num1}%${num2}%${num3}

#output results

echo "num1+num2+num3=${res1}"

echo "num1-num2-num3=${res2}"

echo "num1*num2*num3=${res3}"

echo "num1/num2/num3=${res4}"

echo "num1%num2%num3=${res5}"

[root@ChangerLee 运算比较符]#sh let_op.sh 

num1+num2+num3=121

num1-num2-num3=91

num1*num2*num3=5300

num1/num2/num3=2

num1%num2%num3=1



注意num1/num2/num3得到结果为整数2,说明let只能保留整数结果并将小数部分截取掉

Shell实例2:

[root@ChangerLee 运算比较符]#cat let_op_v1.sh 

#!/bin/bash

# let and while

 

i=10

num=1

 

while [ 1 ]

do

if [ $num -le $i ]

then 

echo "$num"

else

break

fi

let num=$num+1

done

[root@ChangerLee 运算比较符]#sh let_op_v1.sh 

1

2

3

4

5

6

7

8

9

10

Let不适用于小数之间的运算:

Shell实例3:

<span style="font-size:18px;"><strong>[root@ChangerLee 运算比较符]#let 1+1

[root@ChangerLee 运算比较符]#let 1.1+1

-bash: let: 1.1+1: syntax error: invalid arithmetic operator (error token is ".1+1")</strong></span>


 

Expr命令介绍:

Expr在linux命令中和let功能类似,它作算数运算时,只能进行整数类型的运算,不能保存小数结果。Expr还可以进行字符串之间的运算。

Expr的使用方法如下:

Expr expression1 操作符expression2

操作符必须加’\’用于转义,并且操作符和两个expression之间必须有空格(这一点与let不同)且Expr 不适合小数的运算。

常见的算数操作分类:

加法:+

减法:-

乘法:*

除法:/

取余数:%

Expr使用方法举例:

(1)执行常用的算数运算操作:

Shell实例4:

[root@ChangerLee 运算比较符]#cat expr_op_v1.sh 

#!/bin/bash

#an exmple of expr

 

num1=56

num2=10

num3=5

echo "num1:$num1  num2:$num2  num3:$num3"

#operating num

res1=`expr $num1 \+ $num2 \+ $num3`

res2=`expr $num1 \- $num2 \- $num3`

res3=`expr $num1 \* $num2 \* $num3`

res4=`expr $num1 \/ $num2 \/ $num3`

res5=`expr $num1 \% $num2 \% $num3`

#output results

echo "num1+num2+num3=$res1"

echo "num1-num2-num3=$res2"

echo "num1*num2*num3=$res3"

echo "num1/num2/num3=$res4"

echo "num1%num2%num3=$res5"

[root@ChangerLee 运算比较符]#sh expr_op_v1.sh

num1:56  num2:10  num3:5

num1+num2+num3=71

num1-num2-num3=41

num1*num2*num3=2800

num1/num2/num3=1

num1%num2%num3=1

</pre><p><strong><span style="font-family:Microsoft YaHei;font-size:18px;">Shell实例5:</span></strong></p><p><strong><span style="font-family:Microsoft YaHei;font-size:18px;"></span></strong></p><pre name="code" class="html">[root@ChangerLee 运算比较符]#cat expr_op_v2.sh 

#!/bin/bash

# let and while

 

i=10

num=1

 

while [ 1 ]

do

if [ $num -le $i ]

then 

echo "$num"

else

break

fi

num=`expr $num \+ 1`

done

[root@ChangerLee 运算比较符]#sh expr_op_v2.sh 

1

2

3

4

5

6

7

8

9

10

 



Shell实例6:

[root@ChangerLee 运算比较符]#expr 1 \+ 1

2

[root@ChangerLee 运算比较符]#expr 1.1 \+ 1

expr: non-integer argument


(2)执行常用的字符串操作

1.输出字符串的长度

<strong><span style="font-size:18px;">[root@ChangerLee 运算比较符]#cat expr_str.sh 

#!/bin/bash

#output length str

 

str1="this str length is 21"

str2="blog.csdn.net/changerjjlee"

 

echo '${#str1}'${#str1}

expr length $str2

[root@ChangerLee 运算比较符]#sh expr_str.sh 

${#str1}21

26</span></strong>



注意:expr只能输出不含有空格的字符串

2.取字串的操作

expr substr $string $postion $length 位置编号从1开始

echo ${$string:$postion:$length}位置编号从0开始

Shell实例7:

[root@ChangerLee 运算比较符]#string="abcdefghi"

[root@ChangerLee 运算比较符]#expr substr $string 1 3

abc

[root@ChangerLee 运算比较符]#echo ${string:0:3}

abc



(3)字符串连接的操作

Shell实例8:

<strong>[root@ChangerLee 运算比较符]#cat con_str_v1.sh 

#!/bin/bash

#connection of strings

 

str1="abc"

str2="def ghi"

str3="${str1}$str2"

 

echo "str1=$str1"

echo "str2=$str2"

echo "str3=$str3"

[root@ChangerLee 运算比较符]#sh con_str_v1.sh 

str1=abc

str2=def ghi

str3=abcdef ghi</strong>



 

(4)字符串替换的操作

 

Shell实例9:

<strong><span style="font-size:18px;">[root@ChangerLee 运算比较符]#cat con_str_v2.sh 

#!/bin/bash

 

string="blog.csdn.net/changerjjlee"

echo ${string/c/C}#只替换一次

echo ${string//c/C}#全部替换

[root@ChangerLee 运算比较符]#sh con_str_v2.sh 

blog.Csdn.net/changerjjlee

blog.Csdn.net/Changerjjlee
</span></strong>
 



  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JaysenLeo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值