Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368557
  • 博文数量: 114
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1219
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-07 21:23
文章分类

全部博文(114)

文章存档

2018年(1)

2017年(5)

2016年(87)

2015年(21)

我的朋友

分类: 系统运维

2016-12-23 10:01:21


  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

阅读(9191) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~