百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

go-sh

> 编程语言
开源

例如 Python-sh, 它是一个使用 Go 语言的便捷调用 shell。

1.1K stars0 点赞2 次浏览
访问官网GitHub

工具介绍

例如 Python-sh, 它是一个使用 Go 语言的便捷调用 shell。

go-sh

  • 如果您依赖旧的 API,请参阅 tag: v.0.1

sh: export BUILD_ID=123 go: s = sh.NewSession().SetEnv("BUILD_ID", "123") sh: alias ll='ls -l' go: s = sh.NewSession().Alias('ll', 'ls', '-l') sh: (cd /; pwd) go: sh.Command("pwd", sh.Dir("/")).Run() sh: test -d data || mkdir data go: 如果 ! sh.Test("dir", "data") { sh.Command("mkdir", "data").Run() } sh: cat first second | awk '{print $1}' go: sh.Command("cat", "first", "second").Command("awk", "{print $1}").Run() sh: count=$(echo "one two three" | wc -w) go: count, err := sh.Echo("one two three").Command("wc", "-w").Output()

sh (在 Ubuntu 中): timeout 1s sleep 3 go: c := sh.Command("sleep", "3"); c.Start(); c.WaitTimeout(time.Second) # 默认的 SIGKILL go: out, err := sh.Command("sleep", "3").SetTimeout(time.Second).Output() # 设置会话超时并获取输出 sh: echo hello | cat go: out, err := sh.Command("cat").SetInput("hello").Output() sh: cat # 从标准输入读取 go: out, err := sh.Command("cat").SetStdin(os.Stdin).Output() sh: ls -l > /tmp/listing.txt # 将标准输出写入文件 go: err := sh.Command("ls", "-l").WriteStdout("/tmp/listing.txt") 如果需要保留 env 和 dir,最好创建一个会话

session := sh.NewSession() session.SetEnv("BUILD_ID", "123") session.SetDir("/")

然后调用 cmd

session.Command("echo", "hello").Run()

将 ShowCMD 设置为 true 以便轻松调试

session.ShowCMD = true 默认情况下,管道仅在最后一个命令以非零状态退出时才返回错误。不过,您也可以启用类似于 bash 的 pipefail 选项。在这种情况下,管道将返回错误。

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Go

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言