在命令提示符中执行时,如何处理带引号和不带引号的字符串?

huangapple go评论82阅读模式
英文:

How to deal with quoted and unquotes strings when executing it in command prompt?

问题

// 输入来自一个 JSON 请求,格式如下
{
   "inputString" : "\"C:\\Program Files (x86)\-Zip\z.exe\" x c:\\temp\\test.zip -oc:\\temp\\test"
}

package main

import (
	"fmt"
	"os/exec"
)

func main() {
    // 接收到的输入将是这种格式
	var inputstring string = "\"C:\\Program Files (x86)\-Zip\z.exe\" x c:\\temp\\firmware8.zip -oc:\\temp\\fw"

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

	out, err := cmd.Output()

	fmt.Println("doneee", string(out), "err", err)
}

输出:"C:\Program Files (x86)\7-Zip\7z.exe" 不被识别为内部或外部命令,可执行程序或批处理文件。

"C:\Program Files (x86)\7-Zip\7z.exe" x c:\temp\test.zip -oc:\temp\test - 我需要在命令提示符下运行这个命令,但它只执行了被标记的部分。

由于输入字符串不是静态的(它来自 JSON),所以我们不能将其拆分为参数。

英文:

input comes from an JSON request which looks like

{ 
   "inputString" : "\"C:\\Program Files (x86)\\7-Zip\\7z.exe\" x c:\\temp\\test.zip -oc:\\temp\\test" 
}

package main

import (
	"fmt"
	"os/exec"
)

func main() {
    //Input received will be of this format
	var inputstring string = "\"C:\Program Files (x86)\7-Zip\7z.exe\" x c:\temp\firmware8.zip -oc:\temp\fw"

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

	out, err := cmd.Output()

	fmt.Println("doneee", string(out), "err", err)
}

Output : "'\"C:\Program Files (x86)\7-Zip\7z.exe\\
"'
is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"

"C:\Program Files (x86)\7-Zip\7z.exe" x c:\temp\test.zip -oc:\temp\test - I have to run this command on command prompt but it is just executing the part which is highlighted

As the input string is not static (it comes from a JSON),So we cannot split them into arguments

答案1

得分: 1

你可以使用原始字符串。查看这个教程

var inputstring string = `"C:\Program Files (x86)-Zipz.exe" x c:\temp\test.zip -oc:\temp\test`
英文:

You can use raw string. look this tutorial.

var inputstring string = `"C:\Program Files (x86)-Zipz.exe" x c:\temp\test.zip -oc:\temp\test`

答案2

得分: -2

var inputstring string = ""C:\Program Files (x86)\7-Zip\7z.exe" x c:\temp\firmware8.zip -oc:\temp\fw"
var buf, stderr bytes.Buffer
proc := exec.Command("cmd")
proc.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf(/c "%s", inputstring)}
//添加这行代码有助于将cmd添加为单行

proc.Stderr = &stderr
proc.Stdout = &buf
proc.Start()
time.Sleep(5 * time.Second)
fmt.Println("完成", buf.String(), "错误信息为", stderr.String())

英文:
var inputstring string = "\"C:\\Program Files (x86)\-Zip\z.exe\" x c:\\temp\\firmware8.zip -oc:\\temp\\fw"
var buf, stderr bytes.Buffer
*proc := exec.Command("cmd")
proc.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf(`/c "%s"`, inputstring)}* //Adding this line helped to add the cmd as single line

proc.Stderr = &stderr
proc.Stdout = &buf
proc.Start()
time.Sleep(5 * time.Second)
fmt.Println("doneee", buf.String(), "error is ", stderr.String())

huangapple
  • 本文由 发表于 2022年1月20日 00:18:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/70774126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定