加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

shell计算/比较(整数 浮点数 字符串)expr bc echo let (())

(2012-10-21 22:56:59)
标签:

杂谈

分类: shell
expr(对字符串和整数做些运算)

规则:
  • 用空格隔开每个项。
  • 用 \(反斜杠) 放在 shell 特定的字符前面。(* | & ( ) >  >=   <   <=  
  • 对包含空格和其他特殊字符的字符串要用引号括起来。
  • 整数前面可以放一个一元连字符。在内部,整数被当作 32 位,双互补数。

1,length  计算字符串的长度,我们可以用awk中的length(s)进行计算。我们也可以用echo中的echo ${#string}进行计算,当然也可以expr中的expr length $string 求出字符串的长度。

[root@localhost shell]# string="hello,everyone my name is xiaoming"
[root@localhost shell]# echo ${#string}
34
[root@localhost shell]# expr length "$string"
34
注意:当string字符串中有空格时必须用双引号把$string引起来。

2,index   index $string substring索引命令功能在字符串$string上找出substring中任一字符第一次出现的位置,若找不到则expr index返回0或1。
[root@localhost shell]# string="hello,everyone my name is xiaoming"
[root@localhost shell]# expr index "$string" my
11
[root@localhost shell]# expr index "$string" nihao
1

3,match  match $string substring命令在string字符串中匹配substring字符串,然后返回匹配到的substring字符串的长度,若找不到则返回0。match String1 String2 与 Expression1 : Expression2 相同,所有的模式固定到字符串的开始(也就是说,只有以字符串的第一个字符开始的序列才被正则表达式匹配)。因此 ^ (插入符号) 在这种情况下就不是特殊字符。
一般地,匹配运算符返回匹配的字符个数(失败的时候返回 0)
[root@localhost shell]# string="hello,everyone my name is xiaoming"
[root@localhost shell]# expr match "$string" my
0
[root@localhost shell]# expr match "$string" hell.*
34
[root@localhost shell]# expr match "$string" hell
4
[root@localhost shell]# expr match "$string" small
0

4,substr  在shell中可以用{string:position}和{string:position:length}进行对string字符串中字符的抽取。第一种是从position位置开始抽取直到字符串结束,第二种是从position位置开始抽取长度为length的子串。而用expr中的expr substr $string $position $length同样能实现上述功能。
[root@localhost shell]# string="hello,everyone my name is xiaoming"
[root@localhost shell]# echo ${string:10}
yone my name is xiaoming
[root@localhost shell]# echo ${string:10:5}
yone
[root@localhost shell]# echo ${string:10:10}
yone my na
[root@localhost shell]# expr substr "$string" 10 5
ryone
注意:echo ${string:10:5}和 expr substr "$string" 10 5的区别在于${string:10:5}以0开始标号而expr substr "$string" 10 5以1开始标号。


5、删除字符串和抽取字符串相似${string#substring}为删除string开头处与substring匹配的最短字符子串,而${string##substring}为删除string开头处与substring匹配的最长字符子串。
举例
  • [root@localhost shell]# string="20091111 readnow please"
  • [root@localhost shell]# echo ${string#2*1}
  • 111 readnow please
  • [root@localhost shell]# string="20091111 readnow please"
  • [root@localhost shell]# echo ${string##2*1}
  • readnow please
解析:第一个为删除2和1之间最短匹配,第二个为删除2和1之间的最长匹配。


6、替换子串${string/substring/replacement}表示仅替换一次substring相配字符,而${string//substring/replacement}表示为替换所有的substring相配的子串。
举例
  • [root@localhost shell]# string="you and you with me"
  • [root@localhost shell]# echo ${string/you/me}
  • me and you with me
  • [root@localhost shell]# string="you and you with me"
  • [root@localhost shell]# echo ${string//you/me}
  • me and me with me


let命令
1,shell中的整数值计算
$ i=0;
$ let i++
$ ((i++))
$ expr $i + 1
$ echo $i 1 | awk '{printf $1+$2}'

let i++;
i=$(expr $i + 1)
i=$(echo $i+1|bc)
i=$(echo "$i 1" | awk '{printf $1+$2;}')

2.let,expr,bc都可以用来求模,运算符都是%,而let和bc可以用来求幂,运算符不一样,前者是**,后者是^
//求模
$ expr 5 % 2
1
$ let i=5%2
$ echo $i
1
$ echo 5 % 2 | bc
1
$ ((i=5%2))
$ echo $i
1

//求幂
$ let i=5**2
$ echo $i
25
$ ((i=5**2))
$ echo $i
25
$ echo "5^2" | bc
25

3,浮点预算,let和expr都无法进行浮点运算,但是bc和awk可以。

bc命令

1,交互式
  • bc -l 进入bc
  • scale=10 将小数位定为10位
  • e(1) 计算e的小数点后10位
  • quit 退出bc

2,echo "scale=10;0.46492572708365077*16205" | bc
    echo "obase=2;123"|bc#转换成2进制

3,用百分数的形式输出
printf "result: %.2f%%\n" `echo "scale=3;(6/17)*100"|bc`//前面一个%代表百分数的显示位数,后面两个%打印成一个%,scale=3是计算到小数点后面三位

shell浮点数比较:
shell脚本中浮点数的比较,一般有两种方式,使用bc和awk的方式,下面分别举例介绍:

一,bc
if [ `echo "0.5 > 0.3" | bc` -eq 1 ]; then echo "ok"; else echo "not ok"; fi; 

二,awk
x=3.1; y=3.2; echo "$x $y" | awk '{if ($1 > $2) print $1; else print $2}'

shell比较:
整数比较 
 
-eq        等于,如:if [ "$a" -eq "$b" ] 
-ne        不等于,如:if [ "$a" -ne "$b" ] 
-gt        大于,如:if [ "$a" -gt "$b" ] 
-ge        大于等于,如:if [ "$a" -ge "$b" ] 
-lt        小于,如:if [ "$a" -lt "$b" ] 
-le        小于等于,如:if [ "$a" -le "$b" ] 
       小于(需要双括号),如:(("$a" < "$b")) 
<=        小于等于(需要双括号),如:(("$a" <= "$b")) 
       大于(需要双括号),如:(("$a" > "$b")) 
>=        大于等于(需要双括号),如:(("$a" >= "$b")) 
 
字符串比较 
       等于,如:if [ "$a" = "$b" ] 
==        等于,如:if [ "$a" == "$b" ],与=等价
!=        不等于,如:if [ "$a" != "$b" ] 
       小于,在ASCII字母顺序下.如: 
       大于,在ASCII字母顺序下.如: 
-z        字符串为"null".就是长度为0. 
-n        字符串不为"null" 



0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有