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)
}
輸出 : "'\"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),所以我們不能將它們拆分為引數
uj5u.com熱心網友回復:
您可以使用原始字串。看看這個教程。
var inputstring string = `"C:\Program Files (x86)\7-Zip\7z.exe" x c:\temp\test.zip -oc:\temp\test`
uj5u.com熱心網友回復:
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())
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418892.html
標籤:
