Go os/exec坑
tags: golang
Go os/exec坑
cp: cannot stat ‘./test/*’: No such file or directory
out, err := exec.Command("cp", "./test/*", "/tmp").CombinedOutput()
logrus.Info(cmd)
logrus.Info(string(out))
这里的*
不会被认为是通配符,而是认为是个文件。因为*
作为globbing的功能是shell提供的,golang的os/exec
库直接调用命令,不涉及shell。
https://stackoverflow.com/questions/31467153/golang-failed-exec-command-that-works-in-terminal
使用以下方法避免
cmd := "cp ./test/* /tmp -r"
out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
logrus.Info(cmd)
logrus.Info(string(out))