加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 17:02:28 所属栏目:安全 来源:网络整理
导读:问题 我有这个bash脚本: ACTIVE_DB=$(grep -P "^[ t]*db.active" config.properties | cut -d= -f2 | tr -s " ")echo $ACTIVE_DBif [ "$ACTIVE_DB" = "A" ]then ln -sf config-b.properties config.propertieselse ln -sf config-a.properties config.prop
问题

我有这个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

config-a.properties

db.active = A

config-b.properties

db.active = B

当我运行脚本时,执行硬拷贝(= 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

当我在提示中逐行手动运行它时,我没有遇到麻烦,确实总是创建一个符号链接,并且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

笔记

>其他任何地方都没有打开文件(我是唯一的活动用户,使用该配置的应用程序未运行).
>有时ln -sf会正常运作,但通常的规则是它会制作硬拷贝.
>脚本从另一个目录运行,但是在执行此处的操作之前,cds到config * .properties文件所在的目录.
>脚本的篇幅要长得多,但这是再现错误的最短示例.
> bash版本是4.1.2(它是本地的,所以我不关心shellshock).
> ln版本是8.4.
>操作系统:Red Hat Enterprise Linux Server 6.5版(圣地亚哥).
>用于该文件夹的文件系统:ext4.

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

我怀疑你有一些其他的脚本或代码覆盖了符号链接.例如,sed -i将废弃符号链接.有许多命令和实用程序可以通过创建副本,修改副本,然后将副本移动到原始文件顶部来修改文件,从而破坏原始符号链接.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读