我有一个看起来像这样的文件:
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding yes
#XAuthLocation /usr/X11R6/bin/xauth
#X11DisplayOffset 10
#X11UseLocalhost yes
我正在尝试编写取消注释以下行的脚本:
#X11Forwarding yes
#XAuthLocation /usr/X11R6/bin/xauth
我写了以下脚本:
open(my $in, '<', '/etc/ssh/sshd_config')
or die "Cannot open /etc/ssh/sshd_config $!";
open(my $out, '>', '/etc/ssh/sshd_config2')
or die "/etc/ssh/sshd_config2: $!";
while (<$in>) {
print $out $_ unless /X11Forwarding/ ;
}
$txt='#X11Forwarding yes';
$txt=reverse $txt;
chop($txt);
$txt=reverse $txt;
`echo "$txt \n" >> '/etc/ssh/sshd_config2'`;
`mv '/etc/ssh/sshd_config2' '/etc/ssh/sshd_config'`;
close($in);
close($out);
现在这个脚本按照我想要的方式工作。 X11Forwarding部分是未注释的,但我似乎无法使其工作,因此XAuthLocation部分也未被注释。有没有办法可以在没有X11Forwarding和XAuthLocation段的情况下打印文件?
答案 0 :(得分:2)
如果您已经拥有这两行,并且只想取消注释,
while (<$in>) {
s/#\s*(?=X11Forwarding|XAuthLocation)//;
print $out $_;
}
答案 1 :(得分:2)
好的,这是为了使其正式化,只需将XAuthLocation
部分添加到跳过行:
print $out $_ unless /X11Forwarding|XAuthLocation/;
答案 2 :(得分:0)
单行:
perl -i -pe's/^#(?=X11Forwarding|XAuthLocation)//' /etc/ssh/sshd_config