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

   2016-12-23 0
核心提示:sed 替换、修改链接文件注意问题#系统与版本[root@localhost~]# cat /etc/redhat-releaseCentOS release 6.8 (Final)[root@localhost~]# sed --versionGNU sed version 4.2.1Copyright (C) 2009 Free Software Foundation, Inc.This is free software; see th
  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
 
标签: Sed SELinux
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 用MobaXterm_Personal_12.0远程连接ubuntu虚拟机,并解决报错connection refused
    用MobaXterm_Personal_12.0远程连接ubuntu虚拟
    步骤一,获取虚拟机的IP地址。我这里用的是linux虚拟机,开机前将网络类型设置成“桥接网卡”。然后开启虚拟机。打开终端,输入如下命令,获得ip 192.168.23.149:ifconfig -a步骤二:打开MobaXterm工具,新建一个session:1. 创建一个有权限的用户:  2.
    02-10
  • linux的sed(增删改查)使用方法 linux教程:sed命令的用法
    linux的sed(增删改查)使用方法 linux教程:sed
    sed的增删改查的基本操作参考:https://www.cnblogs.com/0zcl/p/6855740.html01:增(a)2个sed命令,分别是:(这些操作都是在内存中进行的,所以不会 被写入到原文件中,如果需要修改原文件则需要用大 -i )  eg:修改原文件的内容。为了防止修改配置文件,
    02-10
  • SpringBoot和mybatis整合报错:Caused by: org.apache.ibatis.builder.BuilderException: Error creating documen
    SpringBoot和mybatis整合报错:Caused by: org.
    很明显,报错是xml有问题,于是去检查,发现:    由于粗心,保存的时候,按的太频繁,多按了个s在里面,导致启动报错!
    02-10
  • An incompatible version [1.2.10] of the APR
     这个链接的博主写的很详细,直接推荐:https://blog.csdn.net/zhoukikoo/article/details/80532483
    02-10
  • Caused by: org.apache.ibatis.binding.Binding
    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集
    02-10
  • 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
    02-10
  • Caused by: java.lang.ClassNotFoundException:
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils缺少jar包:commons-math3-3.6.jar 
    02-10
  • Mybatis异常:Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节
    Mybatis异常:Caused by: com.sun.org.apache.x
    Mybatis异常:Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效一、异常情况   二、造成原因  主要是字符编码问题三、解决办法   在pom.xml文件中加入如下内容: 1 plugins
    02-10
  • IIS访问站点,出现connection refused
    IIS访问站点,出现connection refused
    排查后,发现是因为使用了代理导致的。 需要设置 Don't use the proxy server for local addresses. 
    02-10
  • Google Chrome 55 Released – Install on RHEL/CentOS 7/6 and Fedora 25-20
    Google Chrome 55 Released – Install on RHEL
    Google Chrome is a freeware web browser developed by Google Inc. Google Chrome team proudly announced the release of Google Chrome 55 on December 01, 2016.The actual version is 55.0.2883.75 for Linux and Mac OS X/Windows operatin
    02-10
点击排行