为什么这个符号链接创建两个实例 [英] Why is this symbolic link created two instances
问题描述
我用我的点文件的安装脚本。我使用创建一个目录来我家的文件夹符号链接。在执行环节天晴,但第二symobolic创建链接,我不能的原因。
I have an install script used for my dotfiles. I am using to create symbolic links of one directory to my home folder. The links execute fine apart but a second symobolic link is created and I cannot reason why.
在项目的文件夹结构看起来像这样
The folder structure in the project looks like this
install.sh
scripts/
shell.sh
shell/
install.sh
要求 shell.sh
和调用命令
ln -s $SCRIPTS_DIR/shell/ $HOME/.shell
$ SCRIPTS_DIR是全路径
$SCRIPTS_DIR is a full path
所以,我得到我的主目录.shell目录链接就好了,但现在我的项目文件夹中有一个额外的符号链接
So I do get a .shell directory in my home directory linked just fine but now my project folder has an extra symbolic link
install.sh
scripts/
shell -> PATH_TO_PROJECT/shell
shell.sh
shell/
任何解释会被AP preciated
Any explanation would be appreciated
推荐答案
想想,当你运行 LN -S
命令两次当它的目标是一个目录,而会发生什么比文件
Think about what happens when you run the ln -s
command twice when its target is a directory rather than a file.
如果 $ HOME / .shell
不存在,那么
ln -s "$SCRIPTS_DIR/shell/" "$HOME/.shell"
...创建它。但是,如果它的已的存在,那么......
...creates it. However, if it already exists, then...
ln -s "$SCRIPTS_DIR/shell/" "$HOME/.shell"
...对待 .shell
作为目的地目录名,要创建不到目的地的完整路径,和内创建一个新的条目的 EM>该目录中。
...treats .shell
as a destination directory name, not a complete path to the destination to be created, and creates a new entry within that directory.
GNU LN
有一些扩展来解决这个使用情况,包括:
GNU ln
has some extensions to fix this usage, including:
-h如果TARGET_FILE或TARGET_DIR是一个符号链接,不遵循它。这是最有用的-f选项,以取代可能指向目录的符号链接。
-h If the target_file or target_dir is a symbolic link, do not follow it. This is most useful with the -f option, to replace a symlink which may point to a directory.
因此,如果一个GNU系统上运行时,可以使用:
Thus, if operating on a GNU system, you could use:
# quotes added for bash compatibility, since question is tagged for both shells
ln -sfh "$SCRIPTS_DIR/shell/" "$HOME/.shell"
否则,只需先检查一下:
Otherwise, just check first:
[[ -e $HOME/.shell ]] || ln -s "$SCRIPTS_DIR/shell/" "$HOME/.shell"
这篇关于为什么这个符号链接创建两个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!