Golang库之shell操作库 Script
script库是什么
script is a Go library for doing the kind of tasks that shell scripts are good at: reading files, executing subprocesses, counting lines, matching strings, and so on.
安装
# 初始化模块(如果尚未初始化)
go mod init mymodule
# 安装脚本包
go install github.com/bitfield/script@latest
示例代码
package main
import (
"fmt"
"github.com/bitfield/script"
)
func main() {
// 使用 script 包创建一个简单的脚本
output, err := script.Exec("echo Hello, World!").String()
// 其他demo
// 执行Ping
// script.Args().ExecForEach("ping -c 1 {{.}}").Stdout()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(output)
}
其他食用方法,可以查阅github中的README.md