...

Text file src/github.com/codeskyblue/go-sh/README.md

Documentation: github.com/codeskyblue/go-sh

     1## go-sh
     2[![wercker status](https://app.wercker.com/status/009acbd4f00ccc6de7e2554e12a50d84/s "wercker status")](https://app.wercker.com/project/bykey/009acbd4f00ccc6de7e2554e12a50d84)
     3[![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/codeskyblue/go-sh)
     4
     5*If you depend on the old api, see tag: v.0.1*
     6
     7install: `go get github.com/codeskyblue/go-sh`
     8
     9Pipe Example:
    10
    11	package main
    12
    13	import "github.com/codeskyblue/go-sh"
    14
    15	func main() {
    16		sh.Command("echo", "hello\tworld").Command("cut", "-f2").Run()
    17	}
    18
    19Because I like os/exec, `go-sh` is very much modelled after it. However, `go-sh` provides a better experience.
    20
    21These are some of its features:
    22
    23* keep the variable environment (e.g. export)
    24* alias support (e.g. alias in shell)
    25* remember current dir
    26* pipe command
    27* shell build-in commands echo & test
    28* timeout support
    29
    30Examples are important:
    31
    32	sh: echo hello
    33	go: sh.Command("echo", "hello").Run()
    34
    35	sh: export BUILD_ID=123
    36	go: s = sh.NewSession().SetEnv("BUILD_ID", "123")
    37
    38	sh: alias ll='ls -l'
    39	go: s = sh.NewSession().Alias('ll', 'ls', '-l')
    40
    41	sh: (cd /; pwd)
    42	go: sh.Command("pwd", sh.Dir("/")).Run()
    43
    44	sh: test -d data || mkdir data
    45	go: if ! sh.Test("dir", "data") { sh.Command("mkdir", "data").Run() }
    46
    47	sh: cat first second | awk '{print $1}'
    48	go: sh.Command("cat", "first", "second").Command("awk", "{print $1}").Run()
    49
    50	sh: count=$(echo "one two three" | wc -w)
    51	go: count, err := sh.Echo("one two three").Command("wc", "-w").Output()
    52
    53	sh(in ubuntu): timeout 1s sleep 3
    54	go: c := sh.Command("sleep", "3"); c.Start(); c.WaitTimeout(time.Second) # default SIGKILL
    55	go: out, err := sh.Command("sleep", "3").SetTimeout(time.Second).Output() # set session timeout and get output)
    56
    57	sh: echo hello | cat
    58	go: out, err := sh.Command("cat").SetInput("hello").Output()
    59
    60	sh: cat # read from stdin
    61	go: out, err := sh.Command("cat").SetStdin(os.Stdin).Output()
    62
    63	sh: ls -l > /tmp/listing.txt # write stdout to file
    64	go: err := sh.Command("ls", "-l").WriteStdout("/tmp/listing.txt")
    65
    66If you need to keep env and dir, it is better to create a session
    67
    68	session := sh.NewSession()
    69	session.SetEnv("BUILD_ID", "123")
    70	session.SetDir("/")
    71	# then call cmd
    72	session.Command("echo", "hello").Run()
    73	# set ShowCMD to true for easily debug
    74	session.ShowCMD = true
    75
    76By default, pipeline returns error only if the last command exit with a non-zero status. However, you can also enable `pipefail` option like `bash`. In that case, pipeline returns error if any of the commands fail and for multiple failed commands, it returns the error of rightmost failed command.
    77
    78	session := sh.NewSession()
    79	session.PipeFail = true
    80	session.Command("cat", "unknown-file").Command("echo").Run()
    81
    82By default, pipelines's std-error is set to last command's std-error. However, you can also combine std-errors of all commands into pipeline's std-error using `session.PipeStdErrors = true`.
    83
    84for more information, it better to see docs.
    85[![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/codeskyblue/go-sh)
    86
    87### contribute
    88If you love this project, starring it will encourage the coder. Pull requests are welcome.
    89
    90support the author: [alipay](https://me.alipay.com/goskyblue)
    91
    92### thanks
    93this project is based on <http://github.com/codegangsta/inject>. thanks for the author.
    94
    95# the reason to use Go shell
    96Sometimes we need to write shell scripts, but shell scripts are not good at working cross platform,  Go, on the other hand, is good at that. Is there a good way to use Go to write shell like scripts? Using go-sh we can do this now.

View as plain text