频道栏目
首页 > 系统 > 浏览器 > 正文
sed修连接文件,有坑
2015-06-05 10:27:22           
收藏   我要投稿

 

-bash-4.1# ll /etc/rc.local 
lrwxrwxrwx. 1 root root 13 Aug 15  2014 /etc/rc.local -> rc.d/rc.local
-bash-4.1# cat /etc/rc.local 
#!/bin/sh
touch /var/lock/subsys/local
route add default gw 192.169.1.1
service iptables stop
echo "hosfore test line"

-bash-4.1# sed -i "/hosfore/s//jane/" /etc/rc.local 
-bash-4.1# cat /etc/rc.local 
#!/bin/sh
touch /var/lock/subsys/local
route add default gw 192.169.1.1
service iptables stop
echo "jane test line"

-bash-4.1# ll /etc/rc.local 
-rwxr-xr-x 1 root root 300 Jun  5 09:47 /etc/rc.local

-bash-4.1# cat /etc/rc.d/rc.local 
#!/bin/sh
touch /var/lock/subsys/local
route add default gw 192.169.1.1
service iptables stop
echo "hosfore test line"

-bash-4.1# rm /etc/rc.local 
-bash-4.1# ln -s /etc/rc.d/rc.local /etc/rc.local
-bash-4.1# ll /etc/rc.local 
lrwxrwxrwx 1 root root 18 Jun  5 09:51 /etc/rc.local -> /etc/rc.d/rc.local
-bash-4.1# cat /etc/rc.local 
#!/bin/sh
touch /var/lock/subsys/local
route add default gw 192.169.1.1
service iptables stop
echo "hosfore test line" 
-bash-4.1# sed -i --follow-symlinks "/hosfore/s//jane/" /etc/rc.local
-bash-4.1# ll /etc/rc.local 
lrwxrwxrwx 1 root root 18 Jun  5 09:51 /etc/rc.local -> /etc/rc.d/rc.local
-bash-4.1# cat /etc/rc.d/rc.local 
#!/bin/sh
touch /var/lock/subsys/local
route add default gw 192.169.1.1
service iptables stop
echo "jane test line"

 

如上所示,使用“ sed -i "/hosfore/s//jane/" /etc/rc.local ”命令修改文件之后,发现这个文件变成普通文件了 ,查询sed的使用手册,可知,如果需要保持软硬链接属性,修改的时候需要加上对应参数:

 

man sed

DESCRIPTION
       Sed  is  a  stream editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

       --follow-symlinks

            follow symlinks when processing in place; hard links will still be broken.

       -i [SUFFIX], --in-place[=SUFFIX]

            edit  files in place (makes backup if extension supplied).  The default operation mode is to break symbolic and hard links.  This can be changed with --follow-symlinks and --copy.

       -c, --copy

            use copy instead of rename when shuffling files in -i mode.  While this will avoid breaking links (symbolic or hard), the resulting editing operation is not atomic.  This is rarely the desired mode; --follow-symlinks is usually enough, and it is both faster and more secure.

 

点击复制链接 与好友分享!回本站首页
相关TAG标签 文件
上一篇:SEO基础:跳出率和退出率分析
下一篇:weblogic环境,应用上传图片报Couldnotinitializeclasssun.awt.X11.XToolkit
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站