Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

sed 替換與修改軟鏈接文件的注意事項

編輯:關於 Android 開發

sed 替換與修改軟鏈接文件的注意事項

本文記錄一個實戰中容易踩坑的問題:使用 sed -i 直接修改軟鏈接文件,會導致鏈接失效。

問題背景

系統與版本:

[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@localhost ~]# sed --version
GNU sed version 4.2.1

由於 /etc/sysconfig/selinux 是 /etc/selinux/config 的軟鏈接,執行 sed -i 修改後發現 SELINUX 並未真正關閉。查看鏈接變化:

[root@localhost ~]# ll /etc/sysconfig/selinux
lrwxrwxrwx. 1 root root 17 Dec 20 11:31 /etc/sysconfig/selinux -> ../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

直接用 -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 ~]# 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 ~]# 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

官方文檔也指出,--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 版本不同:

[root@DNS ~]# sed --version
GNU sed version 4.1.5

CentOS 5 系列使用的舊版 sed 沒有 --follow-symlinks 之類的選項,其 -i 行為與 4.2.1 不同,因此不會出現鏈接被替換成普通文件的問題。使用時務必留意 sed 版本差異帶來的行為變化。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved