This is a creation in Article, where the information may have evolved or changed.
One requirement is to allow the use of pipe characters such as LS | wc-l when calling external commands with the Go language program
the Go language has a certain encapsulation of calls to external commands, and here's a try:
package mainimport ("os""os/exec")func main() {run1()}func run1() {cmd := exec.Command("ls", "|", "wc", "-l")cmd.Stdout = os.Stdoutcmd.Stderr = os.Stderrcmd.Start()cmd.Run()cmd.Wait()}
When this is done, the console will make an error:
ls: Cannot access |: no file or directory
ls: Unable to access WC: no file or directory
Can't Go program use the pipe character command? With this problem, I looked up a lot of information, and finally found that should be written like this (run2):
package mainimport ("os""os/exec")func main() {// run1()run2()}func run1() {cmd := exec.Command("ls", "|", "wc", "-l")cmd.Stdout = os.Stdoutcmd.Stderr = os.Stderrcmd.Start()cmd.Run()cmd.Wait()}func run2() {c1 := exec.Command("ls")c2 := exec.Command("wc", "-l")c2.Stdin, _ = c1.StdoutPipe()c2.Stdout = os.Stdoutc2.Stderr = os.Stderrc2.Start()c1.Run()c2.Wait()}
Haha, this is no problem.
Below, we will point to the advanced, such as query nginx PID Number:
ps-eaf|grep "Nginx:master" |grep-v "grep" |awk ' {print $} '(see RUN3)
Package Mainimport ("OS" "Os/exec") func main () {//RUN1 ()//Run2 () run3 ()}func run1 () {cmd: = exec. Command ("ls", "|", "WC", "-l") cmd. Stdout = OS. Stdoutcmd.stderr = OS. Stderrcmd.start () cmd. Run () cmd. Wait ()}func run2 () {c1: = exec. Command ("ls") C2: = Exec. Command ("WC", "L") C2. Stdin, _ = C1. Stdoutpipe () c2. Stdout = OS. Stdoutc2.stderr = OS. Stderrc2.start () C1. Run () c2. Wait ()}func run3 () {c1: = exec. Command ("PS", "-eaf") C2: = Exec. Command ("grep", ' "Nginx:master" ') C3: = Exec. Command ("grep", "-V", ' "grep") C4: = Exec. Command ("awk", "{print $}") C2. Stdin, _ = C1. Stdoutpipe () C3. Stdin, _ = c2. Stdoutpipe () C4. Stdin, _ = C3. Stdoutpipe () C4. Stdout = OS. Stdoutc4.stderr = OS. Stderrc4.start () C3. Start () c2. Start () C1. Run () C4. Wait ()}
No, it's not.
awk:1: Unexpected character "
awk:1: Unexpected character "
It cannot support the contents of quotes in Parameters!
What to do, is there really no way to do it? God is not a conscientious, finally I found a solution, and more simple:
Package Mainimport ("OS" "Os/exec") func main () {//RUN1 ()//RUN2 ()//Run3 () run4 ()}func run1 () {cmd: = exec. Command ("ls", "|", "WC", "-l") cmd. Stdout = OS. Stdoutcmd.stderr = OS. Stderrcmd.start () cmd. Run () cmd. Wait ()}func run2 () {c1: = exec. Command ("ls") C2: = Exec. Command ("WC", "L") C2. Stdin, _ = C1. Stdoutpipe () c2. Stdout = OS. Stdoutc2.stderr = OS. Stderrc2.start () C1. Run () c2. Wait ()}func run3 () {c1: = exec. Command ("PS", "-eaf") C2: = Exec. Command ("grep", ' "Nginx:master" ') C3: = Exec. Command ("grep", "-V", ' "grep") C4: = Exec. Command ("awk", "{print $}") C2. Stdin, _ = C1. Stdoutpipe () C3. Stdin, _ = c2. Stdoutpipe () C4. Stdin, _ = C3. Stdoutpipe () C4. Stdout = OS. Stdoutc4.stderr = OS. Stderrc4.start () C3. Start () c2. Start () C1. Run () C4. Wait ()}func run4 () {cmd: = exec. Command ("/bin/sh", "-C", ' Ps-eaf|grep "Nginx:master" |grep-v "grep" |awk ' {print $} ') cmd. Stdout = OS. Stdoutcmd.stderr = OS. Stderrcmd.start () cmd. Run () cmd. Wait ()}