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 mainimport ( "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)}输出 : "'\"C:\Program Files (x86)\7-Zip\7z.exe\"'未被识别为内部或外部命令,\r\不可运行的程序或批处理文件。\r\n""C:\Program Files (x86)\7-Zip\7z.exe" xc:\temp\test.zip -oc:\temp\test - 我必须在命令提示符下运行此命令,但它只是在执行部分突出显示由于输入字符串不是静态的(它来自 JSON),所以我们不能将它们拆分为参数
2 回答
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
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)}* //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())
- 2 回答
- 0 关注
- 119 浏览
添加回答
举报
0/150
提交
取消