在学习Linux过程当中,曾经遇到过一些小问题,虽然可能微不足道,可是在追求细节的时候每每会比较纠结(强迫症犯了),空出一个博客文章空间,记录一些细节上的内容,都是很小很简单的东西,不喜勿喷。node
0一、bc计算时浮点问题linux
记的用bc计算数字的时候,若是结果出现小数点,则小数点后内容默认不显示,当时没太在乎。正则表达式
[root@linux-node1 wangdong]# echo "1500/1024" | bc 1 [root@linux-node1 wangdong]# [root@linux-node1 wangdong]# echo "scale=4;1500/1024" | bc 1.4648 [root@linux-node1 wangdong]#
0二、if条件语句判断字符串包含bash
使用了if的正则用法,只不过这里匹配的并不是正则表达式,只是匹配字符串而已
ide
[root@linux-node1 wangdong]# cat test.sh #!/bin/bash A="abcdefg" if [[ $A =~ $1 ]];then echo "suc" fi [root@linux-node1 wangdong]# [root@linux-node1 wangdong]# sh test.sh bcd suc [root@linux-node1 wangdong]# sh test.sh h [root@linux-node1 wangdong]# sh test.sh g suc [root@linux-node1 wangdong]#
0三、不一样系统的bash与sh学习
在CentOS和Redhat系统中,能够查看到bash和sh是同一个东西,他们之间是软连接的关系blog
[root@linux-node1 /]# cat /etc/redhat-release CentOS release 6.7 (Final) [root@linux-node1 /]# ll /bin/sh lrwxrwxrwx 1 root root 4 12月 14 11:15 /bin/sh -> bash
可是在Ubuntu和Debian系统中,bash与sh却不相同。ip
root@debian-node1:~# lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 7.9 (wheezy) Release: 7.9 Codename: wheezy root@debian-node1:~# ls -l /bin/sh lrwxrwxrwx 1 root root 4 Mar 1 2012 /bin/sh -> dash root@debian-node1:~# ls -l /bin/bash -rwxr-xr-x 1 root root 975488 Sep 26 2014 /bin/bash
从效果上也不尽相同,例如在进行排版时候用的 \t ,表明一个tab符字符串
root@debian-node1:~# cat bash.sh #!/bin/bash echo "This\tis\tbash\t!" root@debian-node1:~# bash bash.sh This\tis\tbash\t! root@debian-node1:~# cat sh.sh #!/bin/sh echo "This\tis\tsh\t!" root@debian-node1:~# sh sh.sh This is sh !
若是须要bash支持 \t ,须要在echo上使用-e参数。get
root@debian-node1:~# cat bash.sh #!/bin/bash echo "This\tis\tbash\t!" root@debian-node1:~# bash bash.sh This\tis\tbash\t! root@debian-node1:~# cat bash2.sh #!/bin/bash echo -e "This\tis\tbash\t!" root@debian-node1:~# bash bash2.sh This is bash !
而sh的话,默认使用if的正则格式,会报错,而bash则不会。
root@debian-node1:~# cat sh_if.sh #!/bin/sh A=abcdef if [[ $A =~ $1 ]];then echo "ok" fi root@debian-node1:~# sh sh_if.sh c sh_if.sh: 5: sh_if.sh: [[: not found root@debian-node1:~# root@debian-node1:~# root@debian-node1:~# cat bash_if.sh #!/bin/bash A=abcdef if [[ $A =~ $1 ]];then echo "ok" fi root@debian-node1:~# bash bash_if.sh c ok
关于这点,没有深究,应该有办法支持。
关于sh: http://man.cx/sh
关于bash: http://man.cx/bash
0四、关于计算的使用
A、bc
bc是linux内置的简单计算器,目前我仍是比较喜欢用的,缘由就一个,简单。只是他计算出来的结果若是是一个0-1之间的小数,好比0.5,那么其显示的时候只会显示.5,让我蛋疼不已。
首先是简单的加减乘除计算:
[root@linux-node1 ~]# echo "1234+5678"|bc 6912 [root@linux-node1 ~]# echo "20000-50"|bc 19950 [root@linux-node1 ~]# echo "20*30"|bc 600 [root@linux-node1 ~]# echo "15/2"|bc 7 [root@linux-node1 ~]# echo "scale=2;15/2"|bc 7.50
而后也能够用其进行二、八、十、16进制的转换:(此项转自http://jonly.blog.51cto.com/889501/1536237)
其中ibase中写源进制,obase写转换进制。
[root@linux-node1 ~]# echo "obase=2;ibase=10;15" |bc 1111 [root@linux-node1 ~]# echo "obase=16;ibase=10;15" |bc F
最后是计算次方与平方根
[root@linux-node1 ~]# echo "2^8"|bc 256 [root@linux-node1 ~]# echo "sqrt(9)"|bc 3 [root@linux-node1 ~]# echo "sqrt(10)"|bc 3 [root@linux-node1 ~]# echo "scale=2;sqrt(10)"|bc 3.16