• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Fork me on GitHub
            隨筆 - 215  文章 - 13  trackbacks - 0
            <2017年3月>
            2627281234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678


            專注即時通訊及網(wǎng)游服務(wù)端編程
            ------------------------------------
            Openresty 官方模塊
            Openresty 標(biāo)準(zhǔn)模塊(Opm)
            Openresty 三方模塊
            ------------------------------------
            本博收藏大部分文章為轉(zhuǎn)載,并在文章開頭給出了原文出處,如有再轉(zhuǎn),敬請保留相關(guān)信息,這是大家對原創(chuàng)作者勞動成果的自覺尊重!!如為您帶來不便,請于本博下留言,謝謝配合。

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            相冊

            Awesome

            Blog

            Book

            GitHub

            Link

            搜索

            •  

            積分與排名

            • 積分 - 215351
            • 排名 - 118

            最新評論

            閱讀排行榜

            golang os/exec 執(zhí)行外部命令

            exec包執(zhí)行外部命令,它將os.StartProcess進行包裝使得它更容易映射到stdin和stdout,并且利用pipe連接i/o.

            func LookPath(file string) (string, error) //LookPath在環(huán)境變量中查找科執(zhí)行二進制文件,如果file中包含一個斜杠,則直接根據(jù)絕對路徑或者相對本目錄的相對路徑去查找

            [html] view plain copy
            1. func main() {  
            2.     f, err := exec.LookPath("ls")  
            3.     if err != nil {  
            4.         fmt.Println(err)  
            5.     }  
            6.     fmt.Println(f) //  /bin/ls  
            7. }  


            type Cmd   //表示一個正在準(zhǔn)備或者正在運行的外部命令

            [html] view plain copy
            1. type Cmd struct {  
            2.     Path         string   //運行命令的路徑,絕對路徑或者相對路徑  
            3.     Args         []string   // 命令參數(shù)  
            4.     Env          []string         //進程環(huán)境,如果環(huán)境為空,則使用當(dāng)前進程的環(huán)境  
            5.     Dir          string   //指定command的工作目錄,如果dir為空,則comman在調(diào)用進程所在當(dāng)前目錄中運行  
            6.     Stdin        io.Reader  //標(biāo)準(zhǔn)輸入,如果stdin是nil的話,進程從null device中讀取(os.DevNull),stdin也可以時一個文件,否則的話則在運行過程中再開一個goroutine去  
            7.              //讀取標(biāo)準(zhǔn)輸入  
            8.     Stdout       io.Writer       //標(biāo)準(zhǔn)輸出  
            9.     Stderr       io.Writer  //錯誤輸出,如果這兩個(Stdout和Stderr)為空的話,則command運行時將響應(yīng)的文件描述符連接到os.DevNull  
            10.     ExtraFiles   []*os.File     
            11.     SysProcAttr  *syscall.SysProcAttr  
            12.     Process      *os.Process    //Process是底層進程,只啟動一次  
            13.     ProcessState *os.ProcessState  //ProcessState包含一個退出進程的信息,當(dāng)進程調(diào)用Wait或者Run時便會產(chǎn)生該信息.  
            14. }  

             

            func Command(name string, arg ...string) *Cmd    //command返回cmd結(jié)構(gòu)來執(zhí)行帶有相關(guān)參數(shù)的命令,它僅僅設(shè)定cmd結(jié)構(gòu)中的Path和Args參數(shù),如果name參數(shù)中不包含路徑分隔符,command使用LookPath來解決路徑問題,否則的話就直接使用name;Args直接跟在command命令之后,所以在Args中不許要添加命令.

            [html] view plain copy
            1. func main() {  
            2.     cmd := exec.Command("tr", "a-z", "A-Z")  
            3.     cmd.Stdin = strings.NewReader("some input")  
            4.     var out bytes.Buffer  
            5.     cmd.Stdout = &out  
            6.     err := cmd.Run()  
            7.     if err != nil {  
            8.         log.Fatal(err)  
            9.     }  
            10.     fmt.Printf("in all caps: %q\n", out.String())  //in all caps: "SOME INPUT"  
            11. }  

            func (c *Cmd) CombinedOutput() ([]byte, error) //運行命令,并返回標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤

            [html] view plain copy
            1. func main() {  
            2.     cmd := exec.Command("ls")  //查看當(dāng)前目錄下文件  
            3.     out, err := cmd.CombinedOutput()  
            4.     if err != nil {  
            5.         fmt.Println(err)  
            6.     }  
            7.     fmt.Println(string(out))  
            8. }  



            func (c *Cmd) Output() ([]byte, error)     //運行命令并返回其標(biāo)準(zhǔn)輸出

            [html] view plain copy
            1. func main() {  
            2.     cmd := exec.Command("ls") ///查看當(dāng)前目錄下文件  
            3.     out, err := cmd.Output()  
            4.     if err != nil {  
            5.         fmt.Println(err)  
            6.     }  
            7.     fmt.Println(string(out))  
            8. }  

            注意:Output()和CombinedOutput()不能夠同時使用,因為command的標(biāo)準(zhǔn)輸出只能有一個,同時使用的話便會定義了兩個,便會報錯

            func (c *Cmd) Run() error          //開始指定命令并且等待他執(zhí)行結(jié)束,如果命令能夠成功執(zhí)行完畢,則返回nil,否則的話邊會產(chǎn)生錯誤
            func (c *Cmd) Start() error          //使某個命令開始執(zhí)行,但是并不等到他執(zhí)行結(jié)束,這點和Run命令有區(qū)別.然后使用Wait方法等待命令執(zhí)行完畢并且釋放響應(yīng)的資源

            [html] view plain copy
            1. func main() {  
            2.     cmd := exec.Command("ls")  
            3.     cmd.Stdout = os.Stdout //  
            4.     cmd.Run()  
            5.     fmt.Println(cmd.Start()) //exec: already started  
            6. }  

            注:一個command只能使用Start()或者Run()中的一個啟動命令,不能兩個同時使用.

            func (c *Cmd) StderrPipe() (io.ReadCloser, error)  //StderrPipe返回一個pipe,這個管道連接到command的標(biāo)準(zhǔn)錯誤,當(dāng)command命令退出時,Wait將關(guān)閉這些pipe
            func (c *Cmd) StdinPipe() (io.WriteCloser, error)   //StdinPipe返回一個連接到command標(biāo)準(zhǔn)輸入的管道pipe


            [html] view plain copy
            1. package main  
            2.   
            3. import (  
            4.     "fmt"  
            5.     "os"  
            6.     "os/exec"  
            7. )  
            8.   
            9. func main() {  
            10.     cmd := exec.Command("cat")  
            11.     stdin, err := cmd.StdinPipe()  
            12.     if err != nil {  
            13.         fmt.Println(err)  
            14.     }  
            15.     _, err = stdin.Write([]byte("tmp.txt"))  
            16.     if err != nil {  
            17.         fmt.Println(err)  
            18.     }  
            19.     stdin.Close()  
            20.     cmd.Stdout = os.Stdout     //終端標(biāo)準(zhǔn)輸出tmp.txt  
            21.     cmd.Start()  
            22. }  


            func (c *Cmd) StdoutPipe() (io.ReadCloser, error)        //StdoutPipe返回一個連接到command標(biāo)準(zhǔn)輸出的管道pipe

            [html] view plain copy
            1. func main() {  
            2.     cmd := exec.Command("ls")  
            3.     stdout, err := cmd.StdoutPipe()  //指向cmd命令的stdout  
            4.     cmd.Start()  
            5.     content, err := ioutil.ReadAll(stdout)  
            6.     if err != nil {  
            7.         fmt.Println(err)  
            8.     }  
            9.     fmt.Println(string(content))     //輸出ls命令查看到的內(nèi)容  
            10. }  



            func (c *Cmd) Wait() error             //Wait等待command退出,他必須和Start一起使用,如果命令能夠順利執(zhí)行完并順利退出則返回nil,否則的話便會返回error,其中Wait會是放掉所有與cmd命令相關(guān)的資源


            type Error    //Error返回科執(zhí)行二進制文件名字不能夠執(zhí)行的原因的錯誤

            [html] view plain copy
            1. type Error struct {  
            2.     Name string  
            3.     Err  error  
            4. }  


            func (e *Error) Error() string


            type ExitError  //一個command不能夠正常退出的error

            [html] view plain copy
            1. type ExitError struct {  
            2.     *os.ProcessState  
            3. }  


            func (e *ExitError) Error() string

            posted on 2018-10-09 16:16 思月行云 閱讀(867) 評論(0)  編輯 收藏 引用 所屬分類: Golang
            国产精品久久一区二区三区| 久久久久亚洲?V成人无码| 国产精品热久久无码av| 婷婷综合久久狠狠色99h| 亚洲人成电影网站久久| 久久人人超碰精品CAOPOREN| 久久久久亚洲国产| 久久亚洲国产精品五月天婷| 久久艹国产| 亚洲综合伊人久久综合| 久久青青草原精品国产| 久久精品人人做人人爽电影| 色天使久久综合网天天 | 久久99国产精品99久久| 久久超碰97人人做人人爱| 69国产成人综合久久精品| 久久91精品久久91综合| 精品久久久久久国产三级| 久久综合伊人77777| 精品久久久无码21p发布| 国产精品久久久久久影院 | 国产成人久久精品激情 | 一本色道久久88—综合亚洲精品| 午夜精品久久久久久毛片| 久久久中文字幕日本| 99久久这里只精品国产免费| 国产精品成人无码久久久久久 | 久久精品人妻一区二区三区| 精品久久久久久久久免费影院 | 久久狠狠一本精品综合网| 伊人久久大香线蕉成人| 国产精品久久久久国产A级| 国产精品欧美亚洲韩国日本久久| 久久精品国产亚洲AV蜜臀色欲 | 久久精品中文字幕久久| 综合久久一区二区三区| 欧美粉嫩小泬久久久久久久 | 日本久久中文字幕| 久久综合九色综合精品| 亚洲午夜久久久久久久久久| 国产精品无码久久四虎|