亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在命令提示符下執行時如何處理帶引號和不帶引號的字符串?

在命令提示符下執行時如何處理帶引號和不帶引號的字符串?

Go
互換的青春 2022-10-24 16:55:39
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 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

您可以使用原始字符串??纯?a >這個教程。

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


查看完整回答
反對 回復 2022-10-24
?
搖曳的薔薇

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())


查看完整回答
反對 回復 2022-10-24
  • 2 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號