在这里熟悉Golang,我正在尝试执行shell命令,我需要chmod任何.pem文件,所以我决定使用通配符*func main() { cmd := exec.Command( "chmod", "400", "*.pem" ) cmd.Stdout = os.Stdout cmd.Stderr = os.Stdout if err := cmd.Run(); err != nil { fmt.Println( "Error:", err ) }我一直得到这个错误,而执行:chmod: cannot access '*.pem': No such file or directoryError: exit status 1
2 回答
繁花不似锦
TA贡献1851条经验 获得超4个赞
与来自 C 和其他语言的“系统”库调用不同,os/exec 包有意不调用系统 shell,也不扩展任何 glob 模式或处理通常由 shell 完成的其他扩展、管道或重定向。
所以在这里,不会扩大。作为解决方法,您应该使用 next 来使其正常工作:*
cmd := exec.Command("sh", "-c", "chmod 400 *.pem" )
婷婷同学_
TA贡献1844条经验 获得超8个赞
这是使用os包更改文件权限的另一种方法
filepath.WalkDir(".", func(filePath string, f fs.DirEntry, e error) error {
if e != nil {
log.Fatal(e)
}
if filepath.Ext(f.Name()) == ".pem" {
err := os.Chmod(filePath, 0400)
if err != nil {
log.Fatal(err)
}
fmt.Println(fmt.Sprintf("Successfully changed file permission, file: %s", filePath))
}
return nil
})
完整的代码可以在 https://play.golang.org/p/x_3WsYg4t52 找到
- 2 回答
- 0 关注
- 232 浏览
添加回答
举报
0/150
提交
取消
