为什么这个脚本将同名文件视为相同文件?

为什么这个脚本将同名文件视为相同文件?
#!/usr/bin/bash

install_wm() {
    echo "$(dirname "$0")"
    cd "$(dirname "$0")" && pwd
    mkdir -p /root/.config && cd /root/.config &&
    git clone https://git.suckless.org/dwm && cd dwm && pwd &&
    diff "$(dirname "$0")/config.def.h" /root/.config/dwm
    cp -f "$(dirname "$0")/config.def.h" /root/.config/dwm &&
}

install_wm

当我运行这个脚本root

.
/home/jim/CS/SoftwareDevelopment/MySoftware/Bash/ubuntu-server-LTS
Cloning into 'dwm'...
remote: Enumerating objects: 6504, done.
remote: Counting objects: 100% (6504/6504), done.
remote: Compressing objects: 100% (3216/3216), done.
remote: Total 6504 (delta 3733), reused 5933 (delta 3287), pack-reused 0
Receiving objects: 100% (6504/6504), 6.18 MiB | 8.86 MiB/s, done.
Resolving deltas: 100% (3733/3733), done.
/root/.config/dwm
cp: './config.def.h' and '/root/.config/dwm/config.def.h' are the same file

我将此脚本运行为root. $(dirname "$0")/config.def.h是我的配置文件,它与克隆的存储库中的配置文件具有不同的内容,并且与脚本位于同一目录中。cp './config.def.h' and '/root/.config/dwm/config.def.h' are the same file如果文件只有相同的名称而不是内容,为什么我会得到?此外,当我diff在脚本之外手动运行这两个文件时,我得到的输出显示了它们之间的差异:

22,23c22,23
< static const char *tags[] = { "Brave", "ffplay", "Geany", "Terminal", "5", "6", "7", "8" };
< //https://wiki.gentoo.org/wiki/Dwm#Assigning_applications_to_window_tags
---
> static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
> 
30,34c30,31
<   { "brave-browser", NULL,  NULL,       0,            1,           -1 },
<   { "ffplay",  NULL,       NULL,       1 << 1,       0,           -1 },
<   { "geany",  NULL,       NULL,       1 << 2,       0,           -1 },
<   { "lxterminal",  NULL,       NULL,       1 << 3,       0,           -1 },
<   { "gnome-screenshot",  NULL,NULL,     1 << 4,       1,           -1 },
---
>   { "Gimp",     NULL,       NULL,       0,            1,           -1 },
>   { "Firefox",  NULL,       NULL,       1 << 8,       0,           -1 },
61,63c58
< /* commands 
< https://youtu.be/wRh8HQ4ICwE
< */
---
> /* commands */
66,71c61
< static const char *termcmd[]  = { "lxterminal", NULL };
< static const char *downv[]  = { "amixer", "set", "Master", "3+", NULL };
< static const char *upv[]  = { "amixer", "set", "Master", "3-", NULL };
< static const char *mute[]  = { "amixer", "set", "Master", "toogle", NULL };
< 
< 
---
> static const char *termcmd[]  = { "st", NULL };

diff而从我的脚本中运行时我没有得到任何输出。这里发生了什么?

答案1

用这些行:

mkdir -p /root/.config && cd /root/.config &&
git clone https://git.suckless.org/dwm && cd dwm && pwd &&

执行/root/.config/dwm/该命令时您已进入。cp

正如输出的第一行所示,"$(dirname "$0")"只是.

因此,此时与, 或...cp ./something /root/.config/dwm/相同,您正在将文件复制到自身,这就是抱怨的原因。cp ./something ./cp /root/.config/dwm/something /root/.config/dwm/cp

git您只需告诉克隆到该路径而不是更改目录即可使这变得简单得多:

mkdir -p /root/.config && git clone https://git.suckless.org/dwm /root/.config/dwm

或者甚至只是:

git clone https://git.suckless.org/dwm /root/.config/dwm

正如git创建目录一样。

相关内容