我有这个bash脚本:
ACTIVE_DB=$(grep -P "^[ \t]*db.active" config.properties | cut -d= -f2 | tr -s " ")
echo $ACTIVE_DB
if [ "$ACTIVE_DB" = "A" ]
then
ln -sf config-b.properties config.properties
else
ln -sf config-a.properties config.properties
fi
Run Code Online (Sandbox Code Playgroud)
config-a.properties
db.active = A
Run Code Online (Sandbox Code Playgroud)
config-b.properties
db.active = B
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,执行硬拷贝(= cp)并且config.properties通常不是符号链接(也不是物理链接),而是具有与config-a.properties或相同内容的全新文件config-b.properties.
$ ls -li
53 -rw-r--r-- 1 ogregoir ogregoir 582 Sep 30 15:41 config-a.properties
54 -rw-r--r-- 1 ogregoir ogregoir 582 Sep 30 15:41 config-b.properties
56 -rw-r--r-- 1 ogregoir ogregoir 582 Oct 2 11:28 config.properties
Run Code Online (Sandbox Code Playgroud)
当我在提示中逐行手动运行它时,我没有遇到麻烦,确实总是创建一个符号链接并config.properties指向config-a.properties或config-b.properties.
$ ls -li
53 -rw-r--r-- 1 ogregoir ogregoir 582 Sep 30 15:41 config-a.properties
54 -rw-r--r-- 1 ogregoir ogregoir 582 Sep 30 15:41 config-b.properties
55 lrwxrwxrwx 1 ogregoir ogregoir 20 Oct 2 11:41 config.properties -> config-b.properties
Run Code Online (Sandbox Code Playgroud)
ln -sf行为正常,但通常的规则是它制作硬拷贝.cds到文件所在的目录config*.properties.bash 版本是4.1.2(它是本地的,所以我不关心shellshock).ln 版本是8.4.我怀疑您有一些其他脚本或代码覆盖了符号链接。例如,sed -i将丢弃符号链接。有多种命令和实用程序可以通过创建副本、修改副本,然后将副本移动到原始文件之上来修改文件,这会破坏原始符号链接。