分享好友 系统运维首页 频道列表

sed 替换、修改链接文件注意问题

Linux系统  2016-12-23 16:110
  1. sed 替换、修改链接文件注意问题
  2. #系统与版本
  3. [root@ localhost ~]# cat /etc/redhat-release
  4. CentOS release 6.8 (Final)
  5. [root@ localhost ~]# sed --version
  6. GNU sed version 4.2.1
  7. Copyright (C) 2009 Free Software Foundation, Inc.
  8. This is free software; see the source for copying conditions. There is NO
  9. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
  10. to the extent permitted by law.
  11.  因为sed -i /etc/sysconfig/selinux(selinux文件是/etc/selinux/config的软链接)配置文件重启SELINUX没有关闭,才发现原来sed -i是不能直接修改软链接文件的,如下我修改之后的后果:
  12.  [root@ localhost ~]# ll /etc/sysconfig/selinux
  13. lrwxrwxrwx. 1 root root 17 Dec 20 11:31 /etc/sysconfig/selinux -> ../selinux/config
  14. [root@ localhost ~]# ll /etc/selinux/config
  15. -rw-r--r--. 1 root root 458 Dec 20 11:31 /etc/selinux/config
  16. [root@ localhost ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
  17. [root@ localhost ~]# ll /etc/sysconfig/selinux
  18. -rw-r--r--. 1 root root 457 Dec 22 19:18 /etc/sysconfig/selinux
  19. 我们发现链接文件不再是链接文件了,后来查看sed --help选项时发现如下选项说明
  20. --follow-symlinks
  21.               follow symlinks when processing in place; hard links will still be broken.
  22. -i[SUFFIX], --in-place[ = SUFFIX]
  23.               edit files in place (makes backup if extension supplied). The default operation mode is to
  24.               break symbolic and hard links. This can be changed with --follow-symlinks and --copy.
  25. -c, --copy
  26.               use copy instead of rename when shuffling files in -i mode. While this will avoid breaking
  27.               links (symbolic or hard), the resulting editing operation is not atomic. This is rarely the
  28.               desired mode; --follow-symlinks is usually enough, and it is both faster and more secure.
  29.             
  30.             
  31. #测试
  32. [root@ localhost ~]# echo "test" >>test
  33. [root@ localhost ~]# ln -s test test-zsq
  34. [root@ localhost ~]# ll -i test-zsq #链接文件
  35. 260727 lrwxrwxrwx. 1 root root 4 Dec 22 19:21 test-zsq -> test
  36. [root@ localhost ~]# cat test-zsq
  37. test
  38. #如果直接-i删除,链接文件将失效
  39. [root@ localhost ~]# sed -i "s/test//g" test-zsq
  40. [root@ localhost ~]# ll -i test-zsq
  41. 260726 -rw-r--r--. 1 root root 1 Dec 22 19:29 test-zsq
  42. #重新添加再测试,加上-c选项
  43. [root@ localhost ~]# rm -rf test-zsq
  44. [root@ localhost ~]# ln -s test test-zsq
  45. [root@ localhost ~]# ll -i test-zsq
  46. 260726 lrwxrwxrwx. 1 root root 4 Dec 22 19:33 test-zsq -> test
  47. [root@ localhost ~]# echo "test" >>test
  48. [root@ localhost ~]# sed -i -c '/test/d' test-zsq
  49. [root@ localhost ~]# ll -i test-zsq
  50. 260726 lrwxrwxrwx. 1 root root 4 Dec 22 19:33 test-zsq -> test
  51. #--follow-symlinks选项
  52. [root@ localhost ~]# rm -rf test-zsq
  53. [root@ localhost ~]# ln -s test test-zsq
  54. [root@ localhost ~]# echo "test" >> test
  55. [root@ localhost ~]# cat test
  56. test
  57. [root@ localhost ~]# sed -i --follow-symlinks '/test/d' test
  58. [root@ localhost ~]# ls -l test-zsq
  59. lrwxrwxrwx. 1 root root 4 Dec 22 19:50 test-zsq -> test
  60. [root@ localhost ~]# cat test
  61. --follow-symlinks比-c选项更快更安全
  62.   -c, --copy
  63.                  use copy instead of rename when shuffling files in -i mode.
  64.                  While this will avoid breaking links (symbolic or hard), the
  65.                  resulting editing operation is not atomic. This is rarely
  66.                  the desired mode; --follow-symlinks is usually enough, and
  67.                  it is both faster and more secure.
  68. 链接文件/etc/rc.local也是/etc/rc.d/rc.local的软连接,用sed添加删除启动服务要特别注意
  69. 有可能配置的链接文件失效而导致服务起不来
  70. 在centos 5.x系列中运行相同的操作没有出现类似的现象
  71. 经查是sed的版本不同造成的影响,centos 5系列的还是使用老版本的sed,没有--follow-symlinks类似的选项
  72. [root@DNS ~]# sed --version
  73. GNU sed version 4.1.5
  74. Copyright (C) 2003 Free Software Foundation, Inc.
  75. This is free software; see the source for copying conditions. There is NO
  76. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
  77. to the extent permitted by law.
  78. [root@DNS ~]# sed --help
  79. Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
  80.   -n, --quiet, --silent
  81.                  suppress automatic printing of pattern space
  82.   -e script, --expression = script
  83.                  add the script to the commands to be executed
  84.   -f script-file, --file = script-file
  85.                  add the contents of script-file to the commands to be executed
  86.   -i[SUFFIX], --in-place[ = SUFFIX]
  87.                  edit files in place (makes backup if extension supplied)
  88.   -c, --copy
  89.                  use copy instead of rename when shuffling files in -i mode
  90.          (avoids change of input file ownership)
  91.   -l N, --line-length = N
  92.                  specify the desired line-wrap length for the `l' command
  93.   --posix
  94.                  disable all GNU extensions.
  95.   -r, --regexp-extended
  96.                  use extended regular expressions in the script.
  97.   -s, --separate
  98.                  consider files as separate rather than as a single continuous
  99.                  long stream.
  100.   -u, --unbuffered
  101.                  load minimal amounts of data from the input files and flush
  102.                  the output buffers more often
  103.       --help display this help and exit
  104.       --version output version information and exit

查看更多关于【Linux系统】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
An incompatible version [1.2.10] of the APR based Apache Tomcat Native library is installed, while T
 这个链接的博主写的很详细,直接推荐:https://blog.csdn.net/zhoukikoo/article/details/80532483

0评论2023-02-10809

Caused by: org.apache.ibatis.binding.BindingException: Parameter '__frch_item_0' not found
Caused by: org.apache.ibatis.binding.BindingException: Parameter '__frch_item_0' not found. Available parameters are [list]  解决办法:1.其实这里的批量保存 ,mybatis 的xml文件中的parameterType 要求不高,可以是java.util.List,也可以是list集

0评论2023-02-10850

The APR based Apache Tomcat Native library 异常解决办法
tomat在linux服务器上启动报The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/local/jdk1.6.0_26/jre/lib/i386/server:/usr/local/jdk1.6.0_26/jre/l

0评论2023-02-10585

Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils缺少jar包:commons-math3-3.6.jar 

0评论2023-02-10328

linux sed命令中的正则表达式问号、加号、圆括号等需要转义
linux sed命令内的正则表达式语法分两种,一种叫Basic (BRE) Syntax,另一种叫Extended (ERE) Syntax。默认使用的是BRE。这个BRE就是一个简化版,语法稍微有点不一样,问号、加号、圆括号、花括号和竖线没有特殊含义,就代表字符本身,如果要原本定义的实现特

0评论2023-02-09737

CXF 异常 Caused by: org.apache.cxf.binding.soap.SoapFault: Unexpected wrapper element
报错如下:二月 10, 2020 8:40:35 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass信息: Creating Service {http://impl.webservice/}HelloServiceService from class webservice.impl.HelloServiceclass com.

0评论2023-02-09365

Caused by: java.lang.NoClassDefFoundError: org/apache/neethi/AssertionBuilderFactory
转自:https://blog.csdn.net/iteye_8264/article/details/826410581、错误描述 [plain] view plain copy  print?严重: StandardWrapper.Throwable  org.apache.cxf.bus.extension.ExtensionException      at org.apache.cxf.bus.extension.E

0评论2023-02-09693

[zz]写在KVM (Kernel-based Virtual Machine) 安装成功后
我为什么要安装KVM?1. 受制于VMWare Workstation版本的8GB磁盘文件限制2. VMWare安装文件动则几百MB,更新麻烦3. 被KVM虚拟机的性能吸引,可以看这个测试报告Phoronix - Ubuntu 8.04 KVM BenchmarksPhoronix - Intel Core i7 Virtualization Performance4.

0评论2023-02-09962

Linux sed命令 (stream editor)
SED (stream editor)是一项Linux指令,功能同awk类似,差别在于,sed简单,对列处理的功能要差一些,awk的功能复杂,对列处理的功能比较强大。sed选项部分-i:直接修改读取的档案内容,而不仅仅是输出-n:取消默认输出,只打印处理的内容-f:直接将sed的动作写在

0评论2017-02-05146

GNU Sed 4.3发布,正则表达式提速10倍
昨天GNU Sed发布了最新版4.3,号称正则表达式提速10倍,并优化了非阻塞IO性能。具体更新内容请看http://lists.gnu.org/archive/html/info-gnu/2017-01/msg00000.html下载请前往软件主页https://www.gnu.org/software/sed/流编辑器 是用来从文件读取文本或者从

0评论2017-01-06142

更多推荐