...

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

Documentation: github.com/codeskyblue/go-sh

     1## OLD README
     2First give you a full example, I will explain every command below.
     3
     4	session := sh.NewSession()
     5	session.Env["PATH"] = "/usr/bin:/bin"
     6	session.Stdout = os.Stdout
     7	session.Stderr = os.Stderr
     8	session.Alias("ll", "ls", "-l")
     9	session.ShowCMD = true // enable for debug
    10	var err error
    11	err = session.Call("ll", "/")
    12	if err != nil {
    13		log.Fatal(err)
    14	}
    15	ret, err := session.Capture("pwd", sh.Dir("/home")) # wraper of session.Call
    16	if err != nil {
    17		log.Fatal(err)
    18	}
    19	# ret is "/home\n"
    20	fmt.Println(ret)
    21
    22create a new Session
    23
    24	session := sh.NewSession()
    25
    26use alias like this
    27
    28	session.Alias("ll", "ls", "-l") # like alias ll='ls -l'
    29
    30set current env like this
    31
    32	session.Env["BUILD_ID"] = "123" # like export BUILD_ID=123
    33
    34set current directory
    35
    36	session.Set(sh.Dir("/")) # like cd /
    37
    38pipe is also supported
    39
    40	session.Command("echo", "hello\tworld").Command("cut", "-f2")
    41	// output should be "world"
    42	session.Run()
    43
    44test, the build in command support
    45
    46	session.Test("d", "dir") // test dir
    47	session.Test("f", "file) // test regular file
    48
    49with `Alias Env Set Call Capture Command` a shell scripts can be easily converted into golang program. below is a shell script.
    50
    51	#!/bin/bash -
    52	#
    53	export PATH=/usr/bin:/bin
    54	alias ll='ls -l'
    55	cd /usr
    56	if test -d "local"
    57	then
    58		ll local | awk '{print $1, $NF}'
    59	fi
    60
    61convert to golang, will be
    62
    63	s := sh.NewSession()
    64	s.Env["PATH"] = "/usr/bin:/bin"
    65	s.Set(sh.Dir("/usr"))
    66	s.Alias("ll", "ls", "-l")
    67	if s.Test("d", "local") {
    68		s.Command("ll", "local").Command("awk", "{print $1, $NF}").Run()
    69	}

View as plain text