shell學習筆記(一)

1. 

1)整數計算:

(1)

    $(())  :用於數學計算

    echo $((3+3))

(2)

    expr :運算符兩邊必須有空格,若被$[]包含,則不必考慮空格問題

    expr 2 + 2


2)浮點數計算:

    bc用於計算浮點數

    bc -q

    echo `"3.44*5"|bc`


2.定義數組

x=(1 2 4 100 5)

x[3] = 20

declare -A 數組名

(1)輸出全部echo ${x[*]}  


(2)輸出數組個數:

             ${#x[*]}

             ${#x[@]} 


(3)數組元素替換:

                    echo ${x[@]/100/3}

3.

((變量名=變量名+常量/變量))

((變量名**n))    : 變量的n次方

unset 變量名 :刪除變量

unset x[0]


4.生成隨機數 ,用$$作佔位符

Random = $$

echo $Random


5.時間性能比較

time echo {1,100}

time echo seq 100

time seq 100


6.

    管道兩邊不能有空格

    若引號內多條語句,用分號分開


7. awk

    [root@localhost ~]# awk '{print $0}' /etc/passwd

    root:x:0:0:root:/root:/bin/bash

    bin:x:1:1:bin:/bin:/sbin/nologin

    daemon:x:2:2:daemon:/sbin:/sbin/nologin

    adm:x:3:4:adm:/var/adm:/sbin/nologin

    [root@localhost ~]# awk '{print ""}' /etc/passwd

    

    

    

    

    [root@localhost ~]# awk '{print "hihi"}' /etc/passwd

    hihi

    hihi

    hihi

    hihi

    [root@localhost ~]# awk -F":" '{print $1 ":" $2}' /etc/passwd

    root:x

    bin:x

    daemon:x

    adm:x


8.sed

    1)ns :替換

        壓縮空白:sed  's/^$//g'

    

    2)nd:刪除

1>sed '1d' file 刪除第一行

sed '2,$d' file 刪除第二行到最後一行

2>2.txt

100 200 300

rr  aa bb

ciejo2     

[root@localhost ~]# sed '/[0-9]/d' 2.txt

rr  aa bb


    3) -n :顯示

        sed -n '1p' file   顯示第一行

        sed -n '2,$p' file  顯示第二行到最後一行

        sed -n '/100/p' 1.txt  顯示包含100的行

    

        [root@localhost ~]# sed -n '/100/p' 1.txt

    100 9 19 20

    100 89 70 80

        [root@localhost ~]# sed -n 's/100//p' 1.txt

     9 19 20

     89 70 80

    4)sed -y:對應着替換

    sed -y '/[a-z]/[A-Z]/g'

    5)sed -e:跟一個表達式

    sed -e expression,-e expression = sed -e expression,expression

    

    6)

    'i':前插

    ‘a’:後插

    sed  'na ajjj ' file :第n行增加ajjjj

    [root@localhost ~]# sed '1a abc efg' 1.txt

    100 9 19 20

    abc efg

    100 89 70 80

    300 546 12 00

    

    7)sed 'nc ssiejie' file :第n行內容 替換爲ssiejie

    [root@localhost ~]# sed '1c ssie' 1.txt

    ssie

    100 89 70 80

    300 546 12 00

 

    8)匹配子串

sed '/\([0-9]\{\+3\}\)/abc\1/' 

[root@localhost ~]# echo seven EGIGHT |sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'

EGIGHT seven

[root@localhost ~]# echo 8349aFde|sed 's/[0-9]+\([a-zA-Z]\+\)/\1/'    

8349aFde

[root@localhost ~]# echo 8349aFde|sed 's/\([0-9]\+\)+\([a-zA-Z]\+\)/\1/'

8349aFde

[root@localhost ~]# echo 8349aFde|sed 's/\([0-9]\+\)\([a-zA-Z]\+\)/\1/' 

8349

9.tee :讀取輸入的數據,並且輸出到文件

    1)tee -a file:追加到文件末尾

    2)tee file:不加-a:覆蓋文件

    

    [root@localhost ~]# who|tee who.txt

    root     pts/3        2015-02-25 10:37 (113.31.32.123)

    root     pts/5        2015-02-27 10:19 (113.31.32.123)

    [root@localhost ~]# cat who.txt

    root     pts/3        2015-02-25 10:37 (113.31.32.123)

    root     pts/5        2015-02-27 10:19 (113.31.32.123)

    [root@localhost ~]# pwd |tee -a who.txt

    /root

    [root@localhost ~]# cat who.txt

    root     pts/3        2015-02-25 10:37 (113.31.32.123)

    root     pts/5        2015-02-27 10:19 (113.31.32.123)

    /root

    

    

    3)tee -   :輸出到屏幕兩次

    4)tee - -  :輸出到屏幕三次

    輸出到屏幕兩次 並寫入兩個文件

    [root@localhost ~]# pwd |tee who.txt who2.txt -

    /root

    /root

    [root@localhost ~]# cat who2.txt

    /root

    [root@localhost ~]# pwd |tee who.txt who2.txt - -

    /root

    /root

    /root

10.xargs

    1)xargs -n [num] :一行顯示幾列

    2)xargs -p '':每行輸出都問用戶“”

    [root@localhost ~]# find / -type f| xargs -n 1 -p echo 

    echo /sbin/cryptsetup ?...y

    echo /sbin/badblocks ?.../sbin/cryptsetup

    

    echo /sbin/mcstransd ?...n

    echo /sbin/telinit ?...n

    echo /sbin/rpcbind ?...^C

    

    3)xargs -I {} cp {} .

    

    [root@localhost ~]# find /etc -name *.txt |xargs -n 1

    /etc/sysconfig/rhn/sources.rpmforge.txt

    /etc/pki/nssdb/pkcs11.txt

    [root@localhost ~]# find /etc -name "*.txt"|xargs -I {} cp {} .

    [root@localhost ~]# ls

    install.log              pkcs11.txt

    install.log.syslog       sources.rpmforge.txt


11.cut :提取列

1)-f :

2)-b:按字節

3)-c :  按字符

4)cut -b -3,3- file   :-3  表示從第一個字節到第三個字節,

:3-  表示從第三個字節到行尾


    [root@localhost ~]# cat /etc/passwd |cut -d : -f -2

    root:x

    bin:x

    daemon:x

    adm:x

    [root@localhost ~]# cat /etc/passwd |cut -d : -b -2

    cut: an input delimiter may be specified only when operating on fields

    Try `cut --help' for more information.

    [root@localhost ~]# cat '/etc/passwd' | cut -c  1  

    r

    b

    d

    a


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章