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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值