duanbairan4235
2019-07-27 05:24
浏览 467
已采纳

执行Windows命令时,如何阻止Golang用反斜杠替换双引号?

I am writing a program in Golang that will use Mozilla's Thunderbird email client to send email. The Windows command that should be executed is:

 start "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" -compose "to='CloudCoin@Protonmail.com',subject='Subject1',body='Hello'" -offline

My Go code looks like this (command is the one listed above) :

    var command string
    command = `start "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"`
    command += ` -compose "to='` + toAddress + `',`
    command += `subject='Subject1',`
    command += `body='Hello'"`
    command += ` -offline`

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

But I get an error:

Windows cannot find '\\'. Make sure you typed the name correctly, and then try again. 

If I change the code to this (moving the word start):

    var command string
    command = ` "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"`
    command += ` -compose "to='` + toAddress + `',`
    command += `subject='Subject1',`
    command += `body='Hello'"`
    command += ` -offline`

    fmt.Println("Command: " + command)

    cmd := exec.Command("cmd.exe", "/C", "start", command)

Then I get another error:

Windows cannot find 'Files'. Make sure you typed the name correctly, and then try again. 

It seems that instead of trying to start "" it is trying to start \\. How can I keep my double-quotes?

图片转代码服务由CSDN问答提供 功能建议

我正在Golang中编写一个程序,该程序将使用Mozilla的Thunderbird电子邮件客户端发送电子邮件。 应该执行的Windows命令是:

 开始“”“ C:\ Program Files(x86)\ Mozilla Thunderbird \ thunderbird.exe”-撰写“ to ='CloudCoin  @ Protonmail.com',subject ='Subject1',body ='Hello'“ -offline 
   
 
 

我的Go代码如下所示(命令是上面列出的代码) ):

  var命令字符串
 command =`start“”“ C:\ Program Files(x86)\ Mozilla Thunderbird \ thunderbird.exe”`
命令+ =  `-compose“ to ='`+ toAddress +`',`
命令+ =`subject ='Subject1',`
命令+ =`body ='Hello'”`
命令+ =`-offline`  
 
 cmd:= exec.Command(“ cmd.exe”,“ / C”,命令)
   
 
 

但是出现错误:

  Windows找不到“ \\”。 请确保您键入正确的名称,然后再试一次。  
   
 
 

如果我将代码更改为此(移动单词start):

  var命令字符串
  command =`“”“ C:\ Program Files(x86)\ Mozilla Thunderbird \ thunderbird.exe”`
 command + =`-compose“ to ='`+ toAddress +`',`
 command + =`subject  ='Subject1',`
命令+ =`body ='Hello'“`
命令+ =`-offline` 
 
 fmt.Println(” Command:“ +命令)
 
 cmd:=  exec.Command(“ cmd.exe”,“ / C”,“开始”,命令)
   
 
 

然后我得到另一个错误:

  Windows找不到“文件”。 请确保您键入正确的名称,然后再试一次。  
   
 
 

似乎不是尝试启动“”,而是尝试启动\\。 如何保留双引号?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douniao7308 2019-07-27 09:09
    已采纳

    Your problem likely is that every separate string passed to exec.Command is passed on (without parsing it) as a single argument to cmd.exe, which does probably not split given strings either, so you have to do it yourself.

    See this example where the argument is being split up too. You should be able to leave " out since you split it up manually anyway, or write a program for it or run it with an interpreter which does the splitting.

    func do() {
        args := []string{
            "/C",
            "start",
            "",
            `C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe`,
            "-compose",
            "to=" + toAddress + ",subject=Subject1,body=Hello",
            "-offline",
        }
        cmd := exec.Command("cmd.exe", args...)
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题