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
-
sed 替换、修改链接文件注意问题
-
- #系统与版本
-
[root@ localhost
~]# cat /etc/redhat-release
- CentOS release 6.8 (Final)
-
[root@ localhost
~]# sed --version
- GNU sed version 4.2.1
- Copyright (C) 2009 Free Software Foundation, Inc.
- This is free software; see the source for copying conditions. There is NO
- warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
- to the extent permitted by law.
-
- 因为sed -i /etc/sysconfig/selinux(selinux文件是/etc/selinux/config的软链接)配置文件重启SELINUX没有关闭,才发现原来sed -i是不能直接修改软链接文件的,如下我修改之后的后果:
-
[root@ localhost
~]# ll /etc/sysconfig/selinux
- lrwxrwxrwx. 1 root root 17 Dec 20 11:31 /etc/sysconfig/selinux -> ../selinux/config
-
[root@ localhost
~]# ll /etc/selinux/config
- -rw-r--r--. 1 root root 458 Dec 20 11:31 /etc/selinux/config
-
-
-
[root@ localhost
~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g"
/etc/sysconfig/selinux
-
[root@ localhost
~]# ll /etc/sysconfig/selinux
- -rw-r--r--. 1 root root 457 Dec 22 19:18 /etc/sysconfig/selinux
-
- 我们发现链接文件不再是链接文件了,后来查看sed --help选项时发现如下选项说明
-
- --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.
-
-
-
- #测试
-
[root@ localhost
~]# echo "test"
>>test
-
[root@ localhost
~]# ln -s test test-zsq
-
[root@ localhost
~]# ll -i test-zsq #链接文件
- 260727 lrwxrwxrwx. 1 root root 4 Dec 22 19:21 test-zsq -> test
-
[root@ localhost
~]# cat test-zsq
- test
-
- #如果直接-i删除,链接文件将失效
-
[root@ localhost
~]# sed -i "s/test//g"
test-zsq
-
[root@ localhost
~]# ll -i test-zsq
- 260726 -rw-r--r--. 1 root root 1 Dec 22 19:29 test-zsq
-
- #重新添加再测试,加上-c选项
-
[root@ localhost
~]# rm -rf test-zsq
-
[root@ localhost
~]# ln -s test test-zsq
-
[root@ localhost
~]# ll -i test-zsq
- 260726 lrwxrwxrwx. 1 root root 4 Dec 22 19:33 test-zsq -> test
-
[root@ localhost
~]# echo "test"
>>test
-
[root@ localhost
~]# sed -i -c '/test/d' test-zsq
-
[root@ localhost
~]# ll -i test-zsq
- 260726 lrwxrwxrwx. 1 root root 4 Dec 22 19:33 test-zsq -> test
-
- #--follow-symlinks选项
-
[root@ localhost
~]# rm -rf test-zsq
-
[root@ localhost
~]# ln -s test test-zsq
-
[root@ localhost
~]# echo "test"
>> test
-
[root@ localhost
~]# cat test
- test
-
[root@ localhost
~]# sed -i --follow-symlinks '/test/d' test
-
[root@ localhost
~]# ls -l test-zsq
- lrwxrwxrwx. 1 root root 4 Dec 22 19:50 test-zsq -> test
-
[root@ localhost
~]# cat test
-
-
- --follow-symlinks比-c选项更快更安全
- -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.
-
-
- 链接文件/etc/rc.local也是/etc/rc.d/rc.local的软连接,用sed添加删除启动服务要特别注意
- 有可能配置的链接文件失效而导致服务起不来
-
-
- 在centos 5.x系列中运行相同的操作没有出现类似的现象
- 经查是sed的版本不同造成的影响,centos 5系列的还是使用老版本的sed,没有--follow-symlinks类似的选项
-
- [root@DNS ~]# sed --version
- GNU sed version 4.1.5
- Copyright (C) 2003 Free Software Foundation, Inc.
- This is free software; see the source for copying conditions. There is NO
- warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
- to the extent permitted by law.
- [root@DNS ~]# sed --help
- Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-
- -n, --quiet, --silent
- suppress automatic printing of pattern space
-
-e script, --expression =
script
- add the script to the commands to be executed
-
-f script-file, --file =
script-file
- add the contents of script-file to the commands to be executed
-
-i[SUFFIX], --in-place[ =
SUFFIX]
-
edit files
in place (makes backup if extension
supplied)
- -c, --copy
-
use copy instead of rename when shuffling files
in -i mode
- (avoids change of input file ownership)
-
-l N, --line-length =
N
- specify the desired line-wrap length for the `l' command
- --posix
- disable all GNU extensions.
- -r, --regexp-extended
- use extended regular expressions in the script.
- -s, --separate
-
consider files
as separate rather than as a single continuous
- long stream.
- -u, --unbuffered
-
load minimal amounts of data from the input files
and flush
- the output buffers more often
- --help display this help and exit
- --version output version information and exit
免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.