濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Go語(yǔ)言調(diào)用Shell與可執(zhí)行文件的實(shí)現(xiàn)

Go語(yǔ)言調(diào)用Shell與可執(zhí)行文件的實(shí)現(xiàn)

熱門標(biāo)簽:官渡電銷外呼管理系統(tǒng)怎么收費(fèi) 江蘇呼叫中心外呼系統(tǒng)有效果嗎 400開頭電話怎樣申請(qǐng) 杭州人工智能電銷機(jī)器人費(fèi)用 貴州電話智能外呼系統(tǒng) 地圖區(qū)域圖標(biāo)注后導(dǎo)出 利用地圖標(biāo)注位置 谷歌美發(fā)店地圖標(biāo)注 赤峰電銷

os/exec包可用于調(diào)用外部命令,可以使用管道連接輸入輸出,并支持阻塞與非阻塞方式執(zhí)行命令。

os/exec包中關(guān)鍵的類型為Cmd,以下介紹的所有方法皆服務(wù)于該類型:

func Command(name string, arg ...string) *Cmd
方法返回一個(gè)*Cmd, 用于執(zhí)行name指定的程序(攜帶arg參數(shù))

func (c *Cmd) Run() error
執(zhí)行Cmd中包含的命令,阻塞直到命令執(zhí)行完成

func (c *Cmd) Start() error
執(zhí)行Cmd中包含的命令,該方法立即返回,并不等待命令執(zhí)行完成

func (c *Cmd) Wait() error
該方法會(huì)阻塞直到Cmd中的命令執(zhí)行完成,但該命令必須是被Start方法開始執(zhí)行的

func (c *Cmd) Output() ([]byte, error)
執(zhí)行Cmd中包含的命令,并返回標(biāo)準(zhǔn)輸出的切片

func (c *Cmd) CombinedOutput() ([]byte, error)
執(zhí)行Cmd中包含的命令,并返回標(biāo)準(zhǔn)輸出與標(biāo)準(zhǔn)錯(cuò)誤合并后的切片

func (c *Cmd) StdinPipe() (io.WriteCloser, error)
返回一個(gè)管道,該管道會(huì)在Cmd中的命令被啟動(dòng)后連接到其標(biāo)準(zhǔn)輸入

func (c *Cmd) StdoutPipe() (io.ReadCloser, error)
返回一個(gè)管道,該管道會(huì)在Cmd中的命令被啟動(dòng)后連接到其標(biāo)準(zhǔn)輸出

func (c *Cmd) StderrPipe() (io.ReadCloser, error)
返回一個(gè)管道,該管道會(huì)在Cmd中的命令被啟動(dòng)后連接到其標(biāo)準(zhǔn)錯(cuò)誤

普通調(diào)用示例:

調(diào)用Shell命令或可執(zhí)行文件

演示在當(dāng)前目錄創(chuàng)建一個(gè)空文件

package main

import (
  "fmt"
  "os/exec"
)

func main(){
  cmd := exec.Command("touch", "test_file")

  err := cmd.Run()
  if err != nil {
    fmt.Println("Execute Command failed:" + err.Error())
    return
  }

  fmt.Println("Execute Command finished.")
}

一般不建議使用這種默認(rèn)方式調(diào)用Shell腳本:

cmd := exec.Command("my_shell.sh")

因?yàn)檫@種方式實(shí)際的執(zhí)行結(jié)果和命令行執(zhí)行#sh my_shell.sh一樣,如果你的Shell腳本不滿足sh的規(guī)范,就會(huì)調(diào)用失敗。

調(diào)用Shell腳本

設(shè)置bash來(lái)調(diào)用指定Shell腳本,dir_size.sh為我們測(cè)試用的Shell腳本。調(diào)用完成后打印Shell腳本的標(biāo)準(zhǔn)輸出到控制臺(tái)。

package main

import (
  "fmt"
  "os/exec"
)

func main(){
  command := `./dir_size.sh .`
  cmd := exec.Command("/bin/bash", "-c", command)

  output, err := cmd.Output()
  if err != nil {
    fmt.Printf("Execute Shell:%s failed with error:%s", command, err.Error())
    return
  }
  fmt.Printf("Execute Shell:%s finished with output:\n%s", command, string(output))
}

dir_size.sh示例文件內(nèi)容如下,用于輸出當(dāng)前目錄的大?。?/p>

#!/bin/bash
du -h --max-depth=1 $1

Go程序運(yùn)行結(jié)果:

[root@localhost opt]# ll
total 2120
-rwx------. 1 root root   36 Jan 22 16:37 dir_size.sh
-rwx------. 1 root root 2152467 Jan 22 16:39 execCommand
drwxrwxr-x. 11 1000 1000  4096 Jul 12 2017 kibana
drwx------. 2 root root  4096 Jan 16 10:45 sftpuser
drwx------. 3 root root  4096 Jan 22 16:41 upload
[root@localhost opt]# ./execCommand 
Execute Shell:./dir_size.sh . finished with output:
4.0K  ./sftpuser
181M  ./kibana
1.1G  ./upload
1.2G  .

使用輸入輸出Pipe

演示使用管道連接到grep命令的標(biāo)準(zhǔn)輸入,過(guò)濾包含test的字符串,并使用管道連接標(biāo)準(zhǔn)輸出,打印運(yùn)行結(jié)果:

package main

import (
  "fmt"
  "io/ioutil"
  "os/exec"
)

func main(){
  cmd := exec.Command("/bin/bash", "-c", "grep test")

  stdin, _ := cmd.StdinPipe()
  stdout, _ := cmd.StdoutPipe()

  if err := cmd.Start(); err != nil{
    fmt.Println("Execute failed when Start:" + err.Error())
    return
  }

  stdin.Write([]byte("go text for grep\n"))
  stdin.Write([]byte("go test text for grep\n"))
  stdin.Close()

  out_bytes, _ := ioutil.ReadAll(stdout)
  stdout.Close()

  if err := cmd.Wait(); err != nil {
    fmt.Println("Execute failed when Wait:" + err.Error())
    return
  }

  fmt.Println("Execute finished:" + string(out_bytes))
}

Go程序運(yùn)行結(jié)果:

[root@localhost ~]# ./execCommand
Execute finished:go test text for grep

阻塞/非阻塞方式調(diào)用

文章開頭方法介紹處已經(jīng)介紹的很清楚,且前面示例都有涉及,就不另行說(shuō)明了。

參考文檔:
GoLang標(biāo)準(zhǔn)庫(kù)文檔

到此這篇關(guān)于Go語(yǔ)言調(diào)用Shell與可執(zhí)行文件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Go語(yǔ)言調(diào)用Shell內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • linux中用shell快速安裝配置Go語(yǔ)言的開發(fā)環(huán)境

標(biāo)簽:宜春 泰安 鷹潭 保定 河池 松原 武漢 黔西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Go語(yǔ)言調(diào)用Shell與可執(zhí)行文件的實(shí)現(xiàn)》,本文關(guān)鍵詞  語(yǔ)言,調(diào)用,Shell,與,可執(zhí)行,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Go語(yǔ)言調(diào)用Shell與可執(zhí)行文件的實(shí)現(xiàn)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Go語(yǔ)言調(diào)用Shell與可執(zhí)行文件的實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    亚东县| 彰化市| 赤峰市| 开化县| 和林格尔县| 琼海市| 平武县| 尉氏县| 颍上县| 苗栗县| 中宁县| 册亨县| 古田县| 舒城县| 昌吉市| 乐陵市| 原平市| 桃源县| 乌苏市| 新巴尔虎右旗| 逊克县| 屏山县| 桑日县| 吴桥县| 冀州市| 武安市| 临漳县| 阿巴嘎旗| 建昌县| 湟源县| 临泽县| 寿阳县| 玉林市| 工布江达县| 江安县| 西乌珠穆沁旗| 盘山县| 板桥市| 霞浦县| 浦城县| 利川市|