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

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

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

问题

  1. // 输入来自一个 JSON 请求,格式如下
  2. {
  3. "inputString" : "\"C:\\Program Files (x86)\-Zip\z.exe\" x c:\\temp\\test.zip -oc:\\temp\\test"
  4. }
  5. package main
  6. import (
  7. "fmt"
  8. "os/exec"
  9. )
  10. func main() {
  11. // 接收到的输入将是这种格式
  12. var inputstring string = "\"C:\\Program Files (x86)\-Zip\z.exe\" x c:\\temp\\firmware8.zip -oc:\\temp\\fw"
  13. cmd := exec.Command("cmd", "/c", inputstring)
  14. out, err := cmd.Output()
  15. fmt.Println("doneee", string(out), "err", err)
  16. }

输出:"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),所以我们不能将其拆分为参数。

英文:
  1. input comes from an JSON request which looks like
  2. {
  3. "inputString" : "\"C:\\Program Files (x86)\\7-Zip\\7z.exe\" x c:\\temp\\test.zip -oc:\\temp\\test"
  4. }
  5. package main
  6. import (
  7. "fmt"
  8. "os/exec"
  9. )
  10. func main() {
  11. //Input received will be of this format
  12. var inputstring string = "\"C:\Program Files (x86)\7-Zip\7z.exe\" x c:\temp\firmware8.zip -oc:\temp\fw"
  13. cmd := exec.Command("cmd", "/c", inputstring)
  14. out, err := cmd.Output()
  15. fmt.Println("doneee", string(out), "err", err)
  16. }

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

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

  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.

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

英文:
  1. var inputstring string = "\"C:\\Program Files (x86)\-Zip\z.exe\" x c:\\temp\\firmware8.zip -oc:\\temp\\fw"
  2. var buf, stderr bytes.Buffer
  3. *proc := exec.Command("cmd")
  4. proc.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf(`/c "%s"`, inputstring)}* //Adding this line helped to add the cmd as single line
  5. proc.Stderr = &stderr
  6. proc.Stdout = &buf
  7. proc.Start()
  8. time.Sleep(5 * time.Second)
  9. 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:

确定