在 msys 中使用 mklink

Jel*_*Cat 15 windows msys mklink

我知道 Windows 晚于或等于 Vista 提供了mklinkshell 命令。我想从 Msys 终端使用它。知道怎么做吗?

当我mklink在 msys 终端上输入时,它输出sh: mklink: command not found. Msys 仅提供一个假ln实用程序,它似乎与cp.

我尝试编写一个 shell 脚本来打开一个 Windows shell 并mklink在其中运行,但是当我的 shell 脚本尝试执行时cmd /C <instructions>,msys 将 Windows shell 带到当前终端的前台并将其留在那里,而不运行指令。

ak2*_*ak2 16

使用cmd //c mklink直接的MSYS bash命令行上应该工作。

$ cmd //c mklink
Creates a symbolic link.

MKLINK [[/D] | [/H] | [/J]] Link Target

        /D      Creates a directory symbolic link.  Default is a file
                symbolic link.
        /H      Creates a hard link instead of a symbolic link.
        /J      Creates a Directory Junction.
        Link    specifies the new symbolic link name.
        Target  specifies the path (relative or absolute) that the new link
                refers to.
Run Code Online (Sandbox Code Playgroud)

注意:mklink 命令和参数需要作为单个参数提供给 cmd。像这样引用整个命令

cmd //c 'mklink link target'
Run Code Online (Sandbox Code Playgroud)

请注意,该命令通常是

cmd /c 'mklink链接目标'

它可以在 Cygwin 和其他 shell 环境中工作,甚至可以在现有的 CMD 会话中工作。但是,msys 似乎对命令行参数(Windows 命令)进行了处理,并将其解释/cC磁盘根目录的路径名,并将其转换为c:\. //c已发现键入会导致 msys 将/c选项传递给cmd. 请参阅如何从 msys shell 运行内部 cmd 命令?

  • ...你需要转义 `/c` 参数,使用 `//c` 代替。请参阅 http://superuser.com/a/526777/50251。 (2认同)

小智 7

您可以使用 Windows 本机符号链接。要启用它取消注释行:

MSYS=winsymlinks:nativestrict
Run Code Online (Sandbox Code Playgroud)

在 MSYS2 中启动 bat 文件。并以管理员权限运行 MSYS2。

  • 在当前版本中,您可以通过取消注释 Msys2 安装文件夹(默认情况下为 C:\msys64)中的相应 `.ini` 文件(`msys2、ini` 等)中的相应行来设置此项。 (2认同)

Jim*_*myZ 6

MSYS=winsymlinks:nativestrict要求你在提升模式下运行 MSYS2,我对此感到非常不舒服。

该脚本仅在调用时提示UAC提升,当然对于编写脚本来说没有什么用处,但至少它满足了我的需求:

  • 〜/scripts/sh/msys2-ln.sh:

    #!/bin/sh
    if [ "$#" -eq 2 -a "$1" == "-s" ]; then
        TARGET="$2"
        LINK=$(basename "$TARGET")
    elif [ "$#" -eq 3 -a "$1" == "-s" ]; then
        TARGET="$2"
        LINK="$3"
    else
        echo "this weak implementation only supports \`ln -s\`"
        exit
    fi
    
    if [ -d "$TARGET" ]; then
        MKLINK_OPTS="//d"
    fi
    
    TARGET=$(cygpath -w -a "$TARGET")
    LINK=$(cygpath -w -a "$LINK")
    
    echo "$TARGET"
    echo "$LINK"
    cscript //nologo ~/scripts/wsh/run-elevated.js \
        cmd //c mklink $MKLINK_OPTS "$LINK" "$TARGET"
    
    Run Code Online (Sandbox Code Playgroud)
  • 〜/scripts/wsh/run-elevated.js

    var args = WScript.Arguments;
    if(args.length == 0){
        WScript.Echo("nothing to run");
        WScript.Quit(0);
    }
    
    var quoted_args = [];
    for(var i = 1; i < args.length; ++i){
        var arg = args(i); // it's a callable, not array like
        if(arg.indexOf(" ") != -1){
            arg = "\"" + arg + "\"";
        }
        quoted_args.push(arg);
    }
    
    var SHAPP = WScript.CreateObject("shell.application");
    SHAPP.ShellExecute(args(0), quoted_args.join(" "), "", "runas", 1);
    
    Run Code Online (Sandbox Code Playgroud)
  • ~/.bashrc

    # workaround for MSYS2's awkward ln
    if [ $- == *i* -a ! -z $MSYSTEM ]; then
        alias ln='~/scripts/sh/msys2-ln.sh'
        alias ecmd='powershell "start-process cmd.exe \"/k cd /d $(pwd -W)\" -verb runas"'
    fi
    
    Run Code Online (Sandbox Code Playgroud)

额外的 ecmd 别名在当前目录中启动一个提升的 cmd,有时可能很有用,它也可以作为通过 powershell 获取 UAC 提升的示例,如果有人知道如何正确地转义这个东西,我们可以放弃那个 WSH 助手。

  • 这个说法不再正确:“MSYS=winsymlinks:nativestrict 要求你在提升模式下运行 MSYS2,我真的对此感到不舒服。”打开开发者模式。请参阅https://superuser.com/a/1400340/26903 (2认同)

小智 5

Windows 10 现在支持符号链接,无需以管理员身份运行,只要您在 Windows 设置中打开开发人员模式即可。

完成后,您可以在主目录ln中的.bashrc文件中使用一行正常工作!

export MSYS=winsymlinks:nativestrict
Run Code Online (Sandbox Code Playgroud)

(我无法确定等效的 .ini 文件在我的 Git Bash 安装中的位置。但是在那里设置环境变量也可能会起作用。)

Josh Kelley 撰写的这篇非常有用的文章中的更多详细信息:https : //www.joshkel.com/2018/01/18/symlinks-in-windows/