如何在特定文件夹中运行Shell命令

如何在特定文件夹中运行Shell命令,第1张

如何在特定文件夹中运行Shell命令

exec.Command()
返回一个type值
*exec.Cmd
Cmd
是一个struct并具有一个
Dir
字段

// Dir specifies the working directory of the command.// If Dir is the empty string, Run runs the command in the// calling process's current directory.Dir string

因此,只需在调用之前进行设置

Cmd.Output()

cmd:= exec.Command("git", "log")cmd.Dir = "your/intended/working/directory"out, err := cmd.Output()

另请注意,这特定于

git
命令;
git
允许您使用该
-C
标志传递路径,因此您也可以这样 *** 作:

out, err := exec.Command("git", "-C", "your/intended/working/directory", "log").    Output()


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5170890.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存