尽管非常尴尬,但可以在以下位置创建符号链接:MSysGit https://github.com/msysgit/.
首先,我们需要确保我们使用的是 Windows。这是一个用于检查的示例函数:
windows() { [[ -n "$WINDIR" ]]; }
现在,我们做不到cmd /C
,因为 MSysGit 会与这个论点通奸并将其变成C:
。另外,不要试图使用/K
;它只有在你没有的情况下才有效K:
drive.
因此,虽然它将在程序参数上替换该值,但不会在此处文档上替换。我们可以利用这一点来发挥我们的优势:
if windows; then
cmd <<< "mklink /D \"${link%/}\" \"${target%/}\"" > /dev/null
else
ln -s "$target" "$link"
fi
另:请注意,我包括了/D
因为我只对目录符号链接感兴趣; Windows 就有这样的区别。通过大量的努力,你可以写一个ln() { ... }
包装 Windows API 并作为完整的嵌入式解决方案的函数,但这...留给读者作为练习。
作为对接受答案的感谢,这里有一个更全面的功能。
# We still need this.
windows() { [[ -n "$WINDIR" ]]; }
# Cross-platform symlink function. With one parameter, it will check
# whether the parameter is a symlink. With two parameters, it will create
# a symlink to a file or directory, with syntax: link $linkname $target
link() {
if [[ -z "$2" ]]; then
# Link-checking mode.
if windows; then
fsutil reparsepoint query "$1" > /dev/null
else
[[ -h "$1" ]]
fi
else
# Link-creation mode.
if windows; then
# Windows needs to be told if it's a directory or not. Infer that.
# Also: note that we convert `/` to `\`. In this case it's necessary.
if [[ -d "$2" ]]; then
cmd <<< "mklink /D \"$1\" \"${2//\//\\}\"" > /dev/null
else
cmd <<< "mklink \"$1\" \"${2//\//\\}\"" > /dev/null
fi
else
# You know what? I think ln's parameters are backwards.
ln -s "$2" "$1"
fi
fi
}
另请注意以下几点:
- 我刚刚写了这个并对其进行了简短的测试Windows 7的 https://en.wikipedia.org/wiki/Windows_7 and Ubuntu https://en.wikipedia.org/wiki/Ubuntu_%28operating_system%29,如果您来自 2015 年并使用 Windows 9,请先尝试一下。
-
NTFS https://en.wikipedia.org/wiki/NTFS有重解析点和连接点。我选择重新分析点,因为它更多的是一个实际的符号链接,适用于文件或目录,但连接点的好处是成为一个可用的解决方案Windows XP https://en.wikipedia.org/wiki/Windows_XP,但它仅适用于目录。
- 一些文件系统,FAT https://en.wikipedia.org/wiki/File_Allocation_Table特别是,不支持符号链接。现代 Windows 版本不再支持从它们启动,但 Windows 和 Linux 可以挂载它们。
奖励功能:删除链接。
# Remove a link, cross-platform.
rmlink() {
if windows; then
# Again, Windows needs to be told if it's a file or directory.
if [[ -d "$1" ]]; then
rmdir "$1";
else
rm "$1"
fi
else
rm "$1"
fi
}