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
/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
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0

[root@localhost shsh]# sed '/^\//a \#hello world' /etc/fstab | head -4
/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
#hello world
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0

[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
rOOT:x:0:0:root:/root:/bin/bash

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、取出一個文件路徑的目錄名稱

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