首页 > 文章列表 > 在Golang中运行带有引号的命令

在Golang中运行带有引号的命令

318 2024-02-27
问题内容

我制作了一个简单的程序,它接受用户输入,然后将其作为系统命令执行。

这需要用户输入并将其存储在命令变量中:

fmt.print(">  ")
    
scanner := bufio.newscanner(os.stdin)
scanner.scan()
command := scanner.text()

然后我执行它并打印出 stdout 以及 stderr:

cmd := exec.command("cmd", "/c", command)

cmd.stdout = customoutput{}
cmd.stderr = os.stderr

if err := cmd.run(); err != nil {
    fmt.println("could not run command: ", err)
}

现在,当我执行如下命令时:ipconfig、arp -a、ping,一切都很好,并且在执行时输出会被打印出来,但是当我尝试在命令中使用双引号时,它就会中断。

问题: 我尝试这样做: echo hello world > file.txt 这工作正常,但是一旦我将文件名放在引号中: echo hello world > "file.txt" 我得到退出状态 1 并从 stderr 我得到: 文件名、目录名或卷标语法不正确。

我尝试过:

  • 搜索,甚至询问 chat-gpt 但我不明白为什么会发生这种情况。
  • 在执行命令之前将引号替换为:“或 ^”,但没有任何效果。

顺便说一句,我使用的是 windows 10。

为了更好地理解完整代码:

package main

import (
    "bufio"
    "fmt"
    "os"
    "os/exec"
)

type customOutput struct{}

func (c customOutput) Write(p []byte) (int, error) {
    fmt.Println("received output: ", string(p))
    return len(p), nil
}

func main() {
    fmt.Print(">  ")
    
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    command := scanner.Text()

    cmd := exec.Command("cmd", "/c", command)

    cmd.Stdout = customOutput{}
    cmd.Stderr = os.Stderr

    if err := cmd.Run(); err != nil {
        fmt.Println("could not run command: ", err)
    }
}


正确答案


你需要做

cmd := exec.Command("cmd.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf(`/c "%s"`, command)}
cmd.Stdout = customOutput{}
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
    fmt.Println("could not run command: ", err)
}

然后可能转义命令中的引号。

https://pkg.go.dev/os/exec#command

在 windows 上,进程将整个命令行作为单个字符串接收并进行自己的解析。 command 将 args 组合并引用到命令行字符串中,其算法与使用 commandlinetoargvw 的应用程序兼容(这是最常见的方式)。 *值得注意的例外是 msiexec.exe 和 cmd.exe(以及所有批处理文件),它们具有不同的取消引用算法。 在这些或其他类似情况下,您可以自己进行引用并在 sysprocattr.cmdline 中提供完整的命令行,将 args 留空。