編輯:關於 Android 開發
本文記錄一個實戰中容易踩坑的問題:使用 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 版本差異帶來的行為變化。
從零建置 Android App 開發環境並寫出第一支 HelloWorld
一、建置開發環境 1、先搞定 JDK 環境變數 JDK 是 Java 開發的基礎,Android 自然也不例外。下載完成後,開啟「計算機 → 內容 → 進階系統設定 →
Android 動態資源載入原理與換膚實踐
Android 動態資源載入原理與換膚實踐 一、Resources 是怎麼被造出來的 我們平常取得資源的起點是 getResources(): public Resou
Universal-Image-Loader 是怎麼把 21M 大圖壓到 194K 的
前兩篇分別聊了 UIL 的配置用法與源碼流程,也說清了它「服務器圖片 → 磁碟緩存 → 內存緩存」的完整鏈路,以及 DiscCache 與 MemoryCache 的策略
Android 自學筆記:ProgressBar 與它的兩個分身 SeekBar、RatingBar
前言 ProgressBar 是進度條的基底元件,同時也是 SeekBar 與 RatingBar 的父類別。三者用法高度相似,差別主要在呈現形式與互動方式。把 Prog