**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替换符执行标志一起使用