脚本中的`ln -s`充当`cp`

Oli*_*ire 5 bash ln cp rhel6

问题

我有这个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.propertiesconfig-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.
  • 操作系统:Red Hat Enterprise Linux Server 6.5版(圣地亚哥).
  • 用于该文件夹的文件系统:ext4.

  • 为什么我的脚本不能始终创建符号链接但是制作硬拷贝?
  • 如何在这里强制使用符号链接?

Joh*_*ica 3

我怀疑您有一些其他脚本或代码覆盖了符号链接。例如,sed -i将丢弃符号链接。有多种命令和实用程序可以通过创建副本、修改副本,然后将副本移动到原始文件之上来修改文件,这会破坏原始符号链接。