程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> sed 替換、修改鏈接文件注意問題

sed 替換、修改鏈接文件注意問題

編輯:JAVA綜合教程

sed 替換、修改鏈接文件注意問題



  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. [root@localhost ~]# echo "test" >>test
  31. [root@localhost ~]# ln -s test test-zsq
  32. [root@localhost ~]# ll -i test-zsq #鏈接文件
  33. 260727 lrwxrwxrwx. 1 root root 4 Dec 22 19:21 test-zsq -> test
  34. [root@localhost ~]# cat test-zsq
  35. test

  36. #如果直接-i刪除,鏈接文件將失效
  37. [root@localhost ~]# sed -i "s/test//g" test-zsq
  38. [root@localhost ~]# ll -i test-zsq
  39. 260726 -rw-r--r--. 1 root root 1 Dec 22 19:29 test-zsq

  40. #重新添加再測試,加上-c選項
  41. [root@localhost ~]# rm -rf test-zsq
  42. [root@localhost ~]# ln -s test test-zsq
  43. [root@localhost ~]# ll -i test-zsq
  44. 260726 lrwxrwxrwx. 1 root root 4 Dec 22 19:33 test-zsq -> test
  45. [root@localhost ~]# echo "test" >>test
  46. [root@localhost ~]# sed -i -c '/test/d' test-zsq
  47. [root@localhost ~]# ll -i test-zsq
  48. 260726 lrwxrwxrwx. 1 root root 4 Dec 22 19:33 test-zsq -> test

  49. #--follow-symlinks選項
  50. [root@localhost ~]# rm -rf test-zsq
  51. [root@localhost ~]# ln -s test test-zsq
  52. [root@localhost ~]# echo "test" >> test
  53. [root@localhost ~]# cat test
  54. test
  55. [root@localhost ~]# sed -i --follow-symlinks '/test/d' test
  56. [root@localhost ~]# ls -l test-zsq
  57. lrwxrwxrwx. 1 root root 4 Dec 22 19:50 test-zsq -> test
  58. [root@localhost ~]# cat test


  59. --follow-symlinks比-c選項更快更安全
  60. -c, --copy
  61. use copy instead of rename when shuffling files in -i mode.
  62. While this will avoid breaking links (symbolic or hard), the
  63. resulting editing operation is not atomic. This is rarely
  64. the desired mode; --follow-symlinks is usually enough, and
  65. it is both faster and more secure.


  66. 鏈接文件/etc/rc.local也是/etc/rc.d/rc.local的軟連接,用sed添加刪除啟動服務要特別注意
  67. 有可能配置的鏈接文件失效而導致服務起不來


  68. 在centos 5.x系列中運行相同的操作沒有出現類似的現象
  69. 經查是sed的版本不同造成的影響,centos 5系列的還是使用老版本的sed,沒有--follow-symlinks類似的選項

  70. [root@DNS ~]# sed --version
  71. GNU sed version 4.1.5
  72. Copyright (C) 2003 Free Software Foundation, Inc.
  73. This is free software; see the source for copying conditions. There is NO
  74. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
  75. to the extent permitted by law.
  76. [root@DNS ~]# sed --help
  77. Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  78. -n, --quiet, --silent
  79. suppress automatic printing of pattern space
  80. -e script, --expression=script
  81. add the script to the commands to be executed
  82. -f script-file, --file=script-file
  83. add the contents of script-file to the commands to be executed
  84. -i[SUFFIX], --in-place[=SUFFIX]
  85. edit files in place (makes backup if extension supplied)
  86. -c, --copy
  87. use copy instead of rename when shuffling files in -i mode
  88. (avoids change of input file ownership)
  89. -l N, --line-length=N
  90. specify the desired line-wrap length for the `l' command
  91. --posix
  92. disable all GNU extensions.
  93. -r, --regexp-extended
  94. use extended regular expressions in the script.
  95. -s, --separate
  96. consider files as separate rather than as a single continuous
  97. long stream.
  98. -u, --unbuffered
  99. load minimal amounts of data from the input files and flush
  100. the output buffers more often
  101. --help display this help and exit
  102. --version output version information and exit

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved