iswbm / GolangCodingTime

Go编程时光,一个零基础入门 Golang 的教程
http://golang.iswbm.com
1.27k stars 229 forks source link

c05/c05_02 #57

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

5.2 os/exec 执行命令的五种姿势 — Go编程时光 1.0.0 documentation

https://golang.iswbm.com/c05/c05_02.html

zengguiyun commented 2 years ago

cmd2 := exec.Command("bash", "/home/wangbm/demo.sh") 请教一下,如果这个demo.sh有&、$这些特殊字符的话,能正常使用吗?

magicianlib commented 7 months ago

第二个通配符的问题可以使用 shell 的 -c 命令:

bash -c "ls -l /var/log/*.log"

对应的 go 代码为:

exec.Command("bash", "-c", "ls", "-l", "/var/log/*.log")

可以看下 bash 手册,对 -c 参数的解释如下:

-c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.