iswbm / GolangCodingTime

Go编程时光,一个零基础入门 Golang 的教程

Home Page:http://golang.iswbm.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

c05/c05_02

utterances-bot opened this issue · comments

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

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

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

第二个通配符的问题可以使用 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.