windows Cygwin 的 bash 无法运行“net use /user”命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2238935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:54:29  来源:igfitidea点击:

Cygwin's bash can't run 'net use /user' command?

windowsbashnetworkingcygwin

提问by prosseek

I run 'net use /user:"Someone" \somewhere', and it works well with cmd.exe.

我运行 'net use /user:"Someone" \somewhere',它与 cmd.exe 配合良好。

With the same cmd.exe, run 'bash --login -i' to use the cygwin/bash, and run the same command but I get the error message as follows.

使用相同的 cmd.exe,运行 'bash --login -i' 以使用 cygwin/bash,并运行相同的命令,但我收到如下错误消息。

System error 67 has occurred.

The network name cannot be found.

Why can't I run 'net use /user' command with cygwin/bash?

为什么我不能用 cygwin/bash 运行“net use /user”命令?

回答by karoberts

In cygwin's bash, you need to escape any of those forwardback slashes, as those are interpreted as escape characters.

在 cygwin 的 bash 中,您需要转义任何那些反斜杠,因为它们被解释为转义字符。

try this

尝试这个

net use /user:"Someone" \\\\server\\share

net use /user:"Someone" \\\\server\\share

or use single quotes, which will pass the argument unchanged

或使用单引号,它将传递不变的参数

net use /user:"Someone" '\\server\share'

net use /user:"Someone" '\\server\share'

回答by Jason Pierce

I have ran into problems attempting to use the /delete switch with net use from bash in windows. It seems to be something with the way certain windows commands process command line arguments.

我在尝试使用 /delete 开关与 Windows 中的 bash 的 net use 时遇到了问题。这似乎与某些 Windows 命令处理命令行参数的方式有关。

I thought I could get around it by launching "net use" from a cmd.exe sub shell, but that too appears to be impacted by the argument processing problem. I found a work around for cmd.exe in quoting but could not seem to find the right quoting to use net use directly for the task.

我以为我可以通过从 cmd.exe 子 shell 启动“net use”来解决它,但这似乎也受到参数处理问题的影响。我在引用中找到了 cmd.exe 的解决方法,但似乎找不到正确的引用来直接使用 net use 来完成任务。

$ cmd "/c net use T: /delete"

回答by Flo

In MSYS/Git Bash, parameters starting with a forward slash are converted by POSIX-to-Windowspath conversion
(i.e. /deletelooks like a path and is converted to C:\Program Files\Git\deleteor something alike).

在 MSYS/Git Bash 中,以正斜杠开头的参数通过POSIX-to-Windows路径转换
(即/delete看起来像路径并转换为C:\Program Files\Git\delete或类似的东西)进行转换。

There are two solutions to circumvent this conversion:

有两种解决方案可以规避这种转换:

  1. Double the first slashto avoid POSIX-to-Windows conversion:

    net use //user
    # outputs usage of NET USE
    
    net use T: //delete
    # outputs "T: was deleted successfully."
    
  2. Use temporary environment variable MSYS_NO_PATHCONV=1like so:

    MSYS_NO_PATHCONV=1 net use /user
    # outputs usage of NET USE
    
    MSYS_NO_PATHCONV=1 net use T: /delete
    # outputs "T: was deleted successfully."
    
  1. 将第一个斜杠加倍以避免 POSIX 到 Windows 的转换:

    net use //user
    # outputs usage of NET USE
    
    net use T: //delete
    # outputs "T: was deleted successfully."
    
  2. 像这样使用临时环境变量MSYS_NO_PATHCONV=1

    MSYS_NO_PATHCONV=1 net use /user
    # outputs usage of NET USE
    
    MSYS_NO_PATHCONV=1 net use T: /delete
    # outputs "T: was deleted successfully."
    

Source/Explanation: Bash translates path parameter in Unix format to windows format, need a way to suppress it #577

来源/说明:Bash 将 Unix 格式的路径参数转换为 windows 格式,需要一种方法来抑制它#577

Update: Mention MSYS is used for this answer as suggested in comment from bobbogo.

更新:根据bobbogo 的评论中的建议,提及 MSYS 用于此答案。