shell指令碼知識點彙總

2022-06-26 08:54:11 字數 3188 閱讀 4366

sed中在對內容進行修改時,有時候需要引用外部變數的值或者獲取乙個shell命令執行的結果,以便達到更加可觀的輸出結果

1、sed中使用變數替換

1)sed命令使用雙引號的情況下,使用$var直接引用

[rooot@192 ~]$ cat test.txt

192.168.53.128/contiv/name

[rooot@192 ~]$ ip=192.168.53.77

[rooot@192 ~]$ sed -i "s/192.168.53.128/$ip/g" test.txt

[rooot@192 ~]$ cat test.txt

192.168.53.77/contiv/name

[rooot@192 ~]$

如果替換的變數內容中含有/符號則會提示錯誤,原因是從語法上看,沒有任何問題;但由於變數中包含有「/」作為分隔符,這會和sed的替換操作的分隔符「/」引起混淆;所以,只要不使用「/」做分隔符就可以解決這個問題,如果使用「%」而不是「/」來作為sed的替換操作的分隔符,就不會出錯。其實使用#或%或;作為分隔符也是可以的,只要不會與替換中有相同的而且不是元字元的特殊符號都是可以的

[rooot@192 chenwei]$ path=/home/root

[rooot@192 chenwei]$ cat test.txt

192.168.53.77/contiv/name

[rooot@192 chenwei]$ sed -i "s%192.168.53.77%$path%g" test.txt

[rooot@192 chenwei]$ cat test.txt

/home/root/contiv/name

[rooot@192 chenwei]$ sed -i "s#/home/root#192.168.53.77#g" test.txt

[rooot@192 chenwei]$ cat test.txt

192.168.53.77/contiv/name

[rooot@192 chenwei]$

2)sed命令使用單引號的情況下,使用'"$var"'引用,即變數用雙引號括起來,外面再加上單引號

[rooot@192 chenwei]$ ip=192.168.0.34

[rooot@192 chenwei]$ cat test.txt

192.168.53.77/contiv/name

[rooot@192 chenwei]$ sed -i 's/192.168.53.77/'"$ip"'/g' test.txt

[rooot@192 chenwei]$ cat test.txt

192.168.0.34/contiv/name

[rooot@192 chenwei]$

2、sed中執行外部命令

1)sed命令使用單引號的情況下使用'`shell command`'或者'$(shell command)'引用命令執行的結果

[rooot@192 chenwei]$ cat test.txt

192.168.0.34/contiv/name

[rooot@192 chenwei]$ ip=192.168.0.56

[rooot@192 chenwei]$ sed -i 's/192.168.0.34/'`echo $ip`'/g' test.txt

[rooot@192 chenwei]$ cat test.txt

192.168.0.56/contiv/name

[rooot@192 chenwei]$

或者使用新式的命令

[rooot@192 chenwei]$ cat test.txt

192.168.0.56/contiv/name

[rooot@192 chenwei]$ ip=192.168.0.68

[rooot@192 chenwei]$ sed -i 's/192.168.0.56/'$(echo $ip)'/g' test.txt

[rooot@192 chenwei]$ cat test.txt

192.168.0.68/contiv/name

[rooot@192 chenwei]$

2.sed命令使用雙引號的情況下直接`shell command`或者$(shell command)引用命令執行的結果

[rooot@192 chenwei]$ cat test.txt

192.168.0.68/contiv/name

[rooot@192 chenwei]$ ip=192.168.0.56

[rooot@192 chenwei]$ sed -i "s/192.168.0.68/$(echo $ip)/g" test.txt

[rooot@192 chenwei]$ cat test.txt

192.168.0.56/contiv/name

在sed語句裡面,變數替換或者執行shell命令,雙引號比單引號少繞一些彎子

3、一些小技巧

在每行的頭新增字元,比如"head",命令如下:

sed 's/^/head&/g' test.file

在每行的行尾新增字元,比如「tail」,命令如下:

sed 's/$/&tail/g' test.file

1)"^"代表行首,"$"代表行尾

2)'s/$/&tail/g'中的字元g代表每行出現的字元全部替換,否則只會替換每行第乙個,而不繼續往後找了

4、直接修改檔案的內容

直接編輯檔案選項-i,會匹配test.txt檔案中每一行的第乙個this替換為this:

sed -i 's/this/this/' test.txt

5、shell變數的寫法

$ 變數var的值, 與$var相同

echo $$   # 當然這樣寫 $s1$s2 也行,但最好加上大括號  

6、shell支援邏輯與或的寫法

表示式

[root@localhost ~]# [ 1 -eq 1 ] && echo 'ok'

ok[root@localhost ~]$ [[ 2 < 3 ]] && echo 'ok'

ok[root@localhost ~]$ [[ 2 < 3 && 4 > 5 ]] && echo 'ok'

ok注意: 運算子只是運算子的擴充。能夠支援<,>符號運算不需要轉義符,它還是以字串比較大小。裡面支援邏輯運算子:|| &&

shell 程式設計知識點彙總

程式設計的開始 bin bash 這部分內容是必須寫的,並且在首行,就是宣告這是乙個shell程式。如何執行shell指令碼 第一種是 附許可權給指令碼,直接執行 chmod 755 指令碼名字 例如 chmod 775 hello.sh 接著我們可以使用絕對路徑來執行我們的指令碼,就可以執行指令碼...

Shell指令碼知識點積累

str aaaaaaaaaaaaaa bbbbbbb str1 匹配第乙個,進行擷取 echo new str1 str1 str2 匹配最後乙個,進行擷取 echo new str2 str2 str aaaaaaaaa cc str1 匹配第乙個,進行擷取 echo new str1 str s...

shell指令碼學習知識點 一

1.shell是linux的外殼,是linux的使用者介面,可以為使用者提供輸入命令和引數,並可以得到命令執行結果的環境 2.可執行程式一般有兩種實現方式 一種是二進位制方式 一種是script方式 二進位制方式是先將編寫好的程式進行編譯,變成計算機可是別的指令 然後在執行。這種編譯好的程式只能執行...