**sed中&符號的作用詳解
sed替換中&符號,經常用來在原文本行中增加字符串。下面通過兩個實例講解&符號的作用**
實例一:
優化開機自動啓動服務(不允許用循環語句)。要求開機自動服務只允許network、crond、sshd、rsyslog、iptables、sysstat這幾個服務在三運行級別啓動,其它服務都關閉。
解答:
第一步篩選出我們想要關閉的服務名稱
[root@localhost ~]# chkconfig --list|egrep -v"sshd|rsyslog|network|crond|iptables|sysstat"|awk '{print $1}' abrt-ccpp abrtd acpid atd auditd blk-availability cpuspeed haldaemon ip6tables irqbalance kdump lvm2-monitor mdmonitor messagebus netconsole netfs nfs-rdma ntpd ntpdate postfix psacct quota_nld rdisc rdma restorecond rngd saslauthd smartd svnserve udev-post
第二步拼接出”chkconfig 服務名稱off”的樣式
[root@localhost ~]# chkconfig --list|egrep -v"sshd|rsyslog|network|crond|iptables|sysstat"|awk '{print $1}'|sed's#.*#chkconfig & off#' #=====>這裏&符號就是代表前面.*匹配的到字符 chkconfig abrt-ccpp off chkconfig abrtd off chkconfig acpid off chkconfig atd off chkconfig auditd off chkconfig blk-availability off chkconfig cpuspeed off chkconfig haldaemon off chkconfig ip6tables off chkconfig irqbalance off chkconfig kdump off chkconfig lvm2-monitor off chkconfig mdmonitor off chkconfig messagebus off chkconfig netconsole off chkconfig netfs off chkconfig nfs-rdma off chkconfig ntpd off chkconfig ntpdate off chkconfig postfix off chkconfig psacct off chkconfig quota_nld off chkconfig rdisc off chkconfig rdma off chkconfig restorecond off chkconfig rngd off chkconfig saslauthd off chkconfig smartd off chkconfig svnserve off chkconfig udev-post off
第三步交給bash解釋器執行並檢查結果
[root@localhost ~]# chkconfig --list|egrep -v"sshd|rsyslog|network|crond|iptables|sysstat"|awk '{print $1}'|sed's#.*#chkconfig & off#'|bash [root@localhost ~]# chkconfig |grep 3:On [root@localhost ~]# chkconfig |grep 3:on crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
第二種方法
使用sed替換標誌e代表執行(execute)。該標誌可以將模式空間中的任何內容當做shell命令執行,並把命令執行的結果返回到模式空間。該標誌只有GNU Sed中才可使用。
[root@localhost ~]# chkconfig --list|egrep -v"sshd|rsyslog|network|crond|iptables|sysstat"|awk '{print $1}'|sed's#.*#chkconfig & off#e' [root@localhost ~]# chkconfig |grep 3:on crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
第三種方法:
[root@localhost ~]#chkconfig |egrep -v "sshd|rsyslog|network|crond|sysstat"|awk '{print$1}'|xargs -i chkconfig {} off
實例二:
批量創建10個用戶並設置密碼(不允許使用循環語句)
第一步跟上面優化服務一樣,先創建10個用戶名
[root@localhost ~]# echo linux{01..10}|xargs-n1 linux01 linux02 linux03 linux04 linux05 linux06 linux07 linux08 linux09 linux10
第二步利用sed的&符號拼接出命令
[root@localhost ~]# echo linux{01..10}|xargs-n1|sed 's#.*#useradd &;echo 123456|passwd --stdin &#' useradd linux01;echo 123456|passwd --stdinlinux01 useradd linux02;echo 123456|passwd --stdinlinux02 useradd linux03;echo 123456|passwd --stdinlinux03 useradd linux04;echo 123456|passwd --stdinlinux04 useradd linux05;echo 123456|passwd --stdinlinux05 useradd linux06;echo 123456|passwd --stdinlinux06 useradd linux07;echo 123456|passwd --stdinlinux07 useradd linux08;echo 123456|passwd --stdinlinux08 useradd linux09;echo 123456|passwd --stdinlinux09 useradd linux10;echo123456|passwd --stdin linux10
第三步利用bash 或sed的e替換符執行拼接出來的命令即可
[root@localhost ~]# echo linux{01..10}|xargs-n1|sed 's#.*#useradd &;echo 123456|passwd --stdin &#'|bash Changing password for user linux01. passwd: all authentication tokens updatedsuccessfully. Changing password for user linux02. passwd: all authentication tokens updatedsuccessfully. Changing password for user linux03. passwd: all authentication tokens updatedsuccessfully. Changing password for user linux04. passwd: all authentication tokens updatedsuccessfully. Changing password for user linux05. passwd: all authentication tokens updatedsuccessfully. Changing password for user linux06. passwd: all authentication tokens updatedsuccessfully. Changing password for user linux07. passwd: all authentication tokens updatedsuccessfully. Changing password for user linux08. passwd: all authentication tokens updatedsuccessfully. Changing password for user linux09. passwd: all authentication tokens updatedsuccessfully. Changing password for user linux10. passwd: allauthentication tokens updated successfully.
第四步檢查賬戶是否被建立
[root@localhost ~]# grep linux /etc/passwd;ll/home/ linux01:x:601:601::/home/linux01:/bin/bash linux02:x:602:602::/home/linux02:/bin/bash linux03:x:603:603::/home/linux03:/bin/bash linux04:x:604:604::/home/linux04:/bin/bash linux05:x:605:605::/home/linux05:/bin/bash linux06:x:606:606::/home/linux06:/bin/bash linux07:x:607:607::/home/linux07:/bin/bash linux08:x:608:608::/home/linux08:/bin/bash linux09:x:609:609::/home/linux09:/bin/bash linux10:x:610:610::/home/linux10:/bin/bash total 40 drwx------ 2 linux01 linux01 4096 May 29 00:57 linux01 drwx------ 2 linux02 linux02 4096 May 29 00:57 linux02 drwx------ 2 linux03 linux03 4096 May 29 00:57 linux03 drwx------ 2 linux04 linux04 4096 May 29 00:57 linux04 drwx------ 2 linux05 linux05 4096 May 29 00:57 linux05 drwx------ 2 linux06 linux06 4096 May 29 00:57 linux06 drwx------ 2 linux07 linux07 4096 May 29 00:57 linux07 drwx------ 2 linux08 linux08 4096 May 29 00:57 linux08 drwx------ 2 linux09 linux09 4096 May 29 00:57 linux09 drwx------ 2 linux10 linux10 4096 May 29 00:57 linux10
sed的&符號總結
1) &符號引用的是前面字符串或正則匹配到的結果 2) &符號常用來拼接字符串 3) &符號常和e替換符執行標誌一起使用