Shell操做之細節整理(未完結)

在學習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
相關文章
相關標籤/搜索