天天看點

sed

bash –n file.sh #檢查腳本有無錯誤

bash –x file.sh #腳本執行中輸出,檢查有無錯誤,單步執行

定義腳本退出狀态碼

exit:退出腳本

exit #

沒能指定退出狀态碼,最後一條指令為腳本退出狀态碼

特殊變量

$? 執行上次的指令 $* 參數清單 $# 參數的個數 $@ 參數清單

位置變量

$1,$2 shift 時就自動換掉一個參數 $ help shift shift: shift [n]     shift positional parameters.     rename the positional parameters $n+1,$n+2 ... to $1,$2 ...  if n is     not given, it is assumed to be 1.     exit status:     returns success unless n is negative or greater than $#.

if [ $# –lt 2 ]; then

echo “cacl.sh arg1 arg2” exit 8

fi

echo “the sum is $[$1+$2]’'

sed 流編輯器,awk

sed 基本用法

sed : stream editor     流編輯器

sed : 模式空間

預設不編輯原檔案,僅對模式空間中的資料做處理

usage: sed [option]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent

                 suppress automatic printing of pattern space

  -e script, --expression=script

                 add the script to the commands to be executed

  -f script-file, --file=script-file

                 add the contents of script-file to the commands to be executed

  -i[suffix], --in-place[=suffix]

                 edit files in place (makes backup if extension supplied)

  -c, --copy

                 use copy instead of rename when shuffling files in -i mode

                 (avoids change of input file ownership)

  -l n, --line-length=n

                 specify the desired line-wrap length for the `l' command

  --posix

                 disable all gnu extensions.

  -r, --regexp-extended

                 use extended regular expressions in the script.

  -s, --separate

                 consider files as separate rather than as a single continuous

                 long stream.

  -u, --unbuffered

                 load minimal amounts of data from the input files and flush

                 the output buffers more often

      --help     display this help and exit

      --version  output version information and exit

sed 'addresscommand' file  位址加指令 sed '1,2d' /etc/inittab

sed '3,$d' /etc/inittab

1、startline,endline

    比如1,100

    $ 最後一行

2、/regexp/

    /^root/

3、/pattern1/,/pattern2/

    第一冷飲被pattern1比對的行開始,至第一次被pattern2 比對到的行結束,這中間的所有行

4、linenumber

    指定的行

5、startline, +n

    從startline開始,向後的n行:

sed 'addresscommand' file

    -n:靜默模式,不再預設顯示模式空間中的内容

    -i:直接修改原檔案

    -e script -e script :可以同時執行多個腳本

    -f /path/to/se_cript

command:

    d: 删除符合條件的行

    p:顯示符合條件的行

    a \string :在指定的行後面追加字元

    i  \string :在指定的行後面插入字元

    r file :将指定檔案的内容添加至符合條件處

    w file :将指定範圍的内容儲存到指定位置

    s/pattern/string/:  查找并替換

                 g:全局替換

                 i:忽略字元大小寫

                s///:s###,s@@@ 分割符,可以多種      

[root@localhost shsh]# sed '/^\//p' /etc/fstab

/dev/volgroup00/logvol00 /                       ext3    defaults        1 1

label=/boot             /boot                   ext3    defaults        1 2

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

sysfs                   /sys                    sysfs   defaults        0 0

proc                    /proc                   proc    defaults        0 0

/dev/volgroup00/logvol01 swap                    swap    defaults        0 0

[root@localhost shsh]# sed '/^\//a \#hello world' /etc/fstab | head -4

#hello world

[root@localhost shsh]# sed -n '/root/w /tmp/w.txt' /etc/passwd

[root@localhost shsh]# more /tmp/w.txt

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

operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost shsh]# sed 's/oot/oot/' /etc/passwd

s///:s###,s@@@ 分割符,可以多種      

[root@localhost shsh]# sed 's@/@#@g' /etc/fstab

#dev#volgroup00#logvol00 #                       ext3    defaults        1 1

label=#boot             #boot                   ext3    defaults        1 2

tmpfs                   #dev#shm                tmpfs   defaults        0 0

devpts                  #dev#pts                devpts  gid=5,mode=620  0 0

sysfs                   #sys                    sysfs   defaults        0 0

proc                    #proc                   proc    defaults        0 0

#dev#volgroup00#logvol01 swap                    swap    defaults        0 0

[root@localhost shsh]# more sed.txt

hello world ,i love you

[root@localhost shsh]# sed 's#l..e#&r#g' sed.txt

hello world ,i lover you

練習

1、删除/etc/grub.conf 檔案中行首的空白符

2、替換/etc/inittab 檔案中"id:3:initdefault:" 一行中的數字為5

3、删除/etc/inittab檔案中的空白行

4、删除/etc/inittab檔案中的開頭#号

5、删除某檔案中開頭的#号及後的空白字元,但要求#号後面必須有空白字元

6、删除某檔案中以空白字元後面跟#類的行中的開頭的空白字元及#

7、取出一個檔案路徑的目錄名稱

繼續閱讀