1)只替換第1個子串
格式:$
以前面的school變數作為測試,先確認變數值:
將字串中的第1個r替換為rrrr:[root@svr5 ~]# echo $school
tarena it group.
2)替換全部子串[root@svr5 ~]# echo $
tarrrrena it group.
格式:$
以前面的school變數作為測試,先確認變數值:
將字串中的所有r都替換為rrrr:[root@svr5 ~]# echo $school
tarena it group.
3)應用示例,根據變數值重設主機名[root@svr5 ~]# echo $
tarrrrena it grrrroup.
用法分解,將當前主機名的域字尾替換為「localdomain」:
將當前主機名整個替換為「localhost.localdomain」:[root@svr5 ~]# echo $hostname //確認當前的主機名
svr5.tarena.com
[root@svr5 ~]# echo $
svr5.localdomain //替換後的字串
假設要設定的新主機名儲存在變數myfqdn內,則重設操作如下所示:[root@svr5 ~]# echo $
localhost.localdomain
如果希望恢復為原來的主機名,只要修改變數myfqdn的值,然後再重新執行一遍替換操作即可:[root@svr5 ~]# myfqdn="dbsvr.example.org" //新主機名變數
[root@svr5 ~]# hostname $ //重設操作
[root@svr5 ~]# hostname //確認修改後的主機名
dbsvr.example.org
引入變數來儲存主機名以後,可以使shell指令碼具有更廣泛的適用性。[root@svr5 ~]# myfqdn="svr5.tarena.com" //定義要恢復的主機名
[root@svr5 ~]# hostname $ //重設主機名
[root@svr5 ~]# hostname //確認恢復結果
svr5.tarena.com
字串擷取,分割
關於字串擷取有多種方法適應不同的需求 1.substring擷取 string str fakhkajhdkahkdhfakh substring與substrvar str1 str.substring 0,2 從索引為0的位置開始,擷取長度為2位 alert str1 str1 fa eg st...
字串擷取與分割 三
以處理系統預設的郵箱路徑為例,可直接使用環境變數mail root svr5 echo mail var spool mail root root svr5 echo l mail root root svr5 echo var spool mail root root svr5 echo mail...
C 字串分割 擷取
using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system.text u...