我在 SSH 配置中有这个设置( /etc/ssh/sshd_config
)
AllowUsers root john
Run Code Online (Sandbox Code Playgroud)
我想添加jane
到行尾
AllowUsers root john jane
Run Code Online (Sandbox Code Playgroud)
我试过了
sed -i -e '/AllowUsers/{s/:/ /g;s/.*=//;s/$/ jane/p}' /etc/ssh/sshd_config && cat /etc/ssh/sshd_config
Run Code Online (Sandbox Code Playgroud)
我一直得到这个结果
AllowUsers root john jane
AllowUsers root john jane
Run Code Online (Sandbox Code Playgroud)
为什么会有多余的线?
如果我以某种方式运行该命令两次
我会得到这些结果
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
Run Code Online (Sandbox Code Playgroud)
再次运行 x3 次,将得到这个
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
AllowUsers root john jane
Run Code Online (Sandbox Code Playgroud)
该p
旗s/$/ jane/p
将打印模式空间。然后在循环结束时将再次打印模式空间,从而产生两行。取下p
旗帜。
也就是说,只是:
sed 's/AllowUsers .*/& jane/'
Run Code Online (Sandbox Code Playgroud)