Golang Linux Shell Programming (i)

Source: Internet
Author: User
Tags stdin

1. Invoking system commands

The Exec package executes the external command, which will be OS. The startprocess is packaged to make it easier to map to stdin and stdout and to connect I/O with pipe

funcstring, arg ...string) *Cmd {}

Call system command:

package mainimport (   "os/exec"   "log"   "fmt")func main() {   cmd := exec.Command("ls","-l")   out,err := cmd.CombinedOutput()//标准输出 标准错误 组合   //out, err := cmd.Output()   //Output()和CombinedOutput()不能够同时使用,   // 因为command的标准输出只能有一个,同时使用的话便会定义了两个,便会报错   ifnil {      log.Fatal(err)   }   fmt.Println(string(out))}
2. Interactive Invoke system commands
 PackageMainImport("Os/exec"   "Bufio"   "FMT"   "Log"   "Time")funcMain () {cmd: = Exec.command ("ls","-L") Out,_: =cmd. Stdoutpipe ()//stdoutpipe Returns a piping pipe that is connected to the command standard output   ifERR: = cmd. Start (); Err! =Nil{log. Fatal ("Start Error:%v", err)} f: = Bufio. Newreader (out) for{line,err: = f.readstring (' \ n ')ifErr! =Nil{ Break} FMT. Print (line)} time. Sleep (time. Hour)//cmd. Wait ()   //wait wait for command to exit, he must use with start,   //If the command executes smoothly and exits successfully, nil is returned, otherwise the error is returned, where wait is the release of all the resources associated with the cmd command}

No Wait () will spawn zombie process, 3466 defunct zombie process, wait for corpse

go build cmd.go./cmd[[email protected] ]#ps -ef |grep cmd2root       3466   2539  0 20:37 pts/0    00:00:00 ./cmd2[[email protected] ]#ps -ef |grep lsroot        683      1  0 18:43 ?        00:00:21 /usr/bin/vmtoolsdroot       3470   3466  0 20:37 pts/0    00:00:00 [ls] <defunct>
3. Homemade Bash
 PackageMainImport("Bufio"   "FMT"   "OS"   "Os/exec"   "Strings")funcMain () {host, _: = OS. Hostname () Prompt: = Fmt. Sprintf ([[Email protected]%s]$], host) R: = Bufio. Newscanner (OS. Stdin)//r: = Bufio. Newreader (OS. Stdin)    for{FMT. Print (Prompt)if!r.scan () { Break} Line: = R.text ()//Line, _: = r.readstring (' \ n ')      //Line = strings. Trimspace (line)      if Len(line) = =0{Continue} args: = Strings. Fields (line) cmd: = Exec.command (args[0], args[1:]...) Cmd. Stdin = OS. Stdin cmd. Stdout = OS. Stdout cmd. Stderr = OS. Stderr err: = cmd. Run ()ifErr! =Nil{FMT. PRINTLN (Err)}}}
4. Piping pipe
 PackageMainImport("FMT"   "IO"   "Log"   "OS"   "Os/exec"   "Strings")funcMain () {line: ="ls | grep F "Cmds: = Strings. Split (Line,"|") S1: = Strings. Fields (cmds[0]) S2: = Strings. Fields (cmds[1]) FMT. Println (S1) fmt. PRINTLN (S2) r, W: = Io. Pipe () Cmd1: = Exec.command (s1[0], s1[1:]...) CMD2: = Exec.command (s2[0], s2[1:]...)//cmd1: = Exec.command ("ls")   //CMD2: = Exec.command ("grep", "F")Cmd1. Stdin = OS. Stdin cmd1. Stdout = w cmd2. Stdin = R cmd2. Stdout = OS. Stdout cmd1. Start () cmd2. Start () log. Print ("Start") cmd1. Wait () cmd2. Wait ()}
5.shell.go

Https://github.com/ningxin1718/gosubshell

Golang Linux Shell Programming (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.