Hi, I’m @Luo Zhu
This article was first published on luo Zhu’s official website
This article synchronizes in the public account “luo Zhu early teahouse”, reprint please contact the author.
Creation is not easy, form a habit, quality three even!
The installation
go get github.com/codeskyblue/go-sh
Copy the code
The pipe sample
package main
import "github.com/codeskyblue/go-sh"
func main(a) {
sh.Command("echo"."hello\tworld").Command("cut"."-f2").Run()
}
Copy the code
Because I love OS /exec, go-sh is very similar to it. However, Go-SH provides a much better development experience.
Here are some of its features:
- Keep environment variables (e.g
export
) - Support for aliases (such as in shells)
- Remember the current directory
- piping
- Built-in shell commands
echo
和test
- The timeout support
Examples are important:
-
sh:
echo hello
-
go:
sh.Command("echo", "hello").Run()
-
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:
if ! 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(in ubuntu):
timeout 1s sleep 3
-
go:
c := sh.Command("sleep", "3"); c.Start(); c.WaitTimeout(time.Second) # default SIGKILL
-
go:
out, err := sh.Command("sleep", "3").SetTimeout(time.Second).Output() # set session timeout and get output)
-
sh:
echo hello | cat
-
go:
out, err := sh.Command("cat").SetInput("hello").Output()
-
sh:
cat # read from stdin
-
go:
out, err := sh.Command("cat").SetStdin(os.Stdin).Output()
-
sh:
ls -l > /tmp/listing.txt # write stdout to file
-
go:
err := sh.Command("ls", "-l").WriteStdout("/tmp/listing.txt")
If you need to preserve env and dir, it is best to create a session:
session := sh.NewSession()
session.SetEnv("BUILD_ID"."123")
session.SetDir("/"Then call CMD session.Command("echo"."hello").run () # set ShowCMD totrueTo debug the program session.ShowCMD =true
Copy the code
By default, the pipe returns an error only if the last command exits with a non-zero state. However, you can also enable pipefail options like bash. In this case, if any command fails, the pipe returns an error; For multiple failed commands, the pipe returns the error for the failed command at the far right.
session := sh.NewSession()
session.PipeFail = true
session.Command("cat"."unknown-file").Command("echo").Run()
Copy the code
By default, the standard error of the pipe is set to the standard error of the previous command. However, you can also use session.PipeStdErrors = true to merge all command standard errors into pipe standard errors.
Reasons to use the Go Shell
Sometimes we need to write Shell scripts, but Shell scripts don’t work well across platforms, and Go does. Is there a good way to write a script-like Shell using Go? The answer is to use go-SH to help you work more easily.