-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_exec.htm
242 lines (242 loc) · 18.2 KB
/
os_exec.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="./site.css" rel="stylesheet">
<title>os/exec</title>
</head>
<body>
<div class="container">
<h2 id="pkg-overview">package exec</h2>
<p><code>import "os/exec"</code>
<p>exec包执行外部命令。它包装了os.StartProcess函数以便更容易的修正输入和输出,使用管道连接I/O,以及作其它的一些调整。</p>
<h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">¶</a></h3>
<a href="../main.html"><h3>返回首页</h3></a>
</br>
<li><a href="#pkg-variables">Variables</a></li>
<li><a href="#Error">type Error</a></li>
<ul>
<li><a href="#Error.Error">func (e *Error) Error() string</a></li>
</ul>
<li><a href="#ExitError">type ExitError</a></li>
<ul>
<li><a href="#ExitError.Error">func (e *ExitError) Error() string</a></li>
</ul>
<li><a href="#LookPath">func LookPath(file string) (string, error)</a></li>
<li><a href="#Cmd">type Cmd</a></li>
<ul>
<li><a href="#Command">func Command(name string, arg ...string) *Cmd</a></li>
<li><a href="#Cmd.StdinPipe">func (c *Cmd) StdinPipe() (io.WriteCloser, error)</a></li>
<li><a href="#Cmd.StdoutPipe">func (c *Cmd) StdoutPipe() (io.ReadCloser, error)</a></li>
<li><a href="#Cmd.StderrPipe">func (c *Cmd) StderrPipe() (io.ReadCloser, error)</a></li>
<li><a href="#Cmd.Run">func (c *Cmd) Run() error</a></li>
<li><a href="#Cmd.Start">func (c *Cmd) Start() error</a></li>
<li><a href="#Cmd.Wait">func (c *Cmd) Wait() error</a></li>
<li><a href="#Cmd.Output">func (c *Cmd) Output() ([]byte, error)</a></li>
<li><a href="#Cmd.CombinedOutput">func (c *Cmd) CombinedOutput() ([]byte, error)</a></li>
</ul>
</ul>
<h4 id="pkg-examples">Examples <a class="permalink" href="#pkg-index">¶</a></h4>
<a href="../main.html"><h3>返回首页</h3></a>
</br>
<li><a href="#example-Cmd-Output" onclick="$('#ex-Cmd-Output').addClass('in').removeClass('collapse').height('auto')">Cmd.Output</a></li>
<li><a href="#example-Cmd-Start" onclick="$('#ex-Cmd-Start').addClass('in').removeClass('collapse').height('auto')">Cmd.Start</a></li>
<li><a href="#example-Cmd-StdoutPipe" onclick="$('#ex-Cmd-StdoutPipe').addClass('in').removeClass('collapse').height('auto')">Cmd.StdoutPipe</a></li>
<li><a href="#example-Command" onclick="$('#ex-Command').addClass('in').removeClass('collapse').height('auto')">Command</a></li>
<li><a href="#example-LookPath" onclick="$('#ex-LookPath').addClass('in').removeClass('collapse').height('auto')">LookPath</a></li>
</ul>
<h3 id="pkg-variables">Variables <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>var <span id="ErrNotFound">ErrNotFound</span> = <a href="errors.htm">errors</a>.<a href="errors.htm#New">New</a>("executable file not found in $PATH")</pre>
<p>如果路径搜索没有找到可执行文件时,就会返回本错误。</p>
<h3 id="Error">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#25">Error</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>type Error struct {
<span id="Error.Name">Name</span> <a href="builtin.htm#string">string</a>
<span id="Error.Err">Err</span> <a href="builtin.htm#error">error</a>
}</pre>
<p>Error类型记录执行失败的程序名和失败的原因。</p>
<h4 id="Error.Error">func (*Error) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#30">Error</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (e *<a href="#Error">Error</a>) Error() <a href="builtin.htm#string">string</a></pre>
<h3 id="ExitError">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#329">ExitError</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>type ExitError struct {
*<a href="os.htm">os</a>.<a href="os.htm#ProcessState">ProcessState</a>
}</pre>
<p>ExitError报告某个命令的一次未成功的返回。</p>
<h4 id="ExitError.Error">func (*ExitError) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#333">Error</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (e *<a href="#ExitError">ExitError</a>) Error() <a href="builtin.htm#string">string</a></pre>
<h3 id="LookPath">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/lp_unix.go?name=release#33">LookPath</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func LookPath(file <a href="builtin.htm#string">string</a>) (<a href="builtin.htm#string">string</a>, <a href="builtin.htm#error">error</a>)</pre>
<p>在环境变量PATH指定的目录中搜索可执行文件,如file中有斜杠,则只在当前目录搜索。返回完整路径或者相对于当前目录的一个相对路径。</p>
<div class="panel-group">
<div class="panel panel-default" id="example-LookPath">
<div class="panel-heading" onclick="document.getElementById('ex-LookPath').style.display = document.getElementById('ex-LookPath').style.display=='none'?'block':'none';">Example</div>
<div id="ex-LookPath" class="panel-collapse collapse">
<div class="panel-body">
<pre>
path, err := exec.LookPath("fortune")
if err != nil {
log.Fatal("installing fortune is in your future")
}
fmt.Printf("fortune is available at %s\n", path)
</pre>
</div>
</div>
</div>
</div>
<h3 id="Cmd">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#35">Cmd</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>type Cmd struct {
<span class="com">// Path是将要执行的命令的路径。</span>
<span class="com">//</span>
<span class="com">// 该字段不能为空,如为相对路径会相对于Dir字段。</span><span class="com"></span>
<span id="Cmd.Path">Path</span> <a href="builtin.htm#string">string</a>
<span class="com">// Args保管命令的参数,包括命令名作为第一个参数;如果为空切片或者nil,相当于无参数命令。</span><span class="com"></span>
<span class="com">//</span>
<span class="com">// 典型用法下,Path和Args都应被Command函数设定。</span>
<span id="Cmd.Args">Args</span> []<a href="builtin.htm#string">string</a>
<span class="com">// Env指定进程的环境,如为nil,则是在当前进程的环境下执行。</span>
<span id="Cmd.Env">Env</span> []<a href="builtin.htm#string">string</a>
<span class="com">// Dir指定命令的工作目录。如为空字符串,会在调用者的进程当前目录下执行。</span>
<span id="Cmd.Dir">Dir</span> <a href="builtin.htm#string">string</a>
<span class="com">// Stdin指定进程的标准输入,如为nil,进程会从空设备读取(os.DevNull)</span><span class="com"></span>
<span id="Cmd.Stdin">Stdin</span> <a href="io.htm">io</a>.<a href="io.htm#Reader">Reader</a>
<span class="com">// Stdout和Stderr指定进程的标准输出和标准错误输出。</span>
<span class="com">//</span>
<span class="com">// 如果任一个为nil,Run方法会将对应的文件描述符关联到空设备(os.DevNull)</span><span class="com"></span>
<span class="com">//</span>
<span class="com">// 如果两个字段相同,同一时间最多有一个线程可以写入。</span>
<span id="Cmd.Stdout">Stdout</span> <a href="io.htm">io</a>.<a href="io.htm#Writer">Writer</a>
<span id="Cmd.Stderr">Stderr</span> <a href="io.htm">io</a>.<a href="io.htm#Writer">Writer</a>
<span class="com">// ExtraFiles指定额外被新进程继承的已打开文件流,不包括标准输入、标准输出、标准错误输出。</span>
<span class="com">// 如果本字段非nil,entry i会变成文件描述符3+i。</span>
<span class="com">//</span>
<span class="com">// BUG: 在OS X 10.6系统中,子进程可能会继承不期望的文件描述符。</span>
<span class="com">// http://golang.org/issue/2603</span>
<span id="Cmd.ExtraFiles">ExtraFiles</span> []*<a href="os.htm">os</a>.<a href="os.htm#File">File</a>
<span class="com">// SysProcAttr保管可选的、各操作系统特定的sys执行属性。</span>
<span class="com">// Run方法会将它作为os.ProcAttr的Sys字段传递给os.StartProcess函数。</span>
<span id="Cmd.SysProcAttr">SysProcAttr</span> *<a href="syscall.htm">syscall</a>.<a href="syscall.htm#SysProcAttr">SysProcAttr</a>
<span class="com">// Process是底层的,只执行一次的进程。</span>
<span id="Cmd.Process">Process</span> *<a href="os.htm">os</a>.<a href="os.htm#Process">Process</a>
<span class="com">// ProcessState包含一个已经存在的进程的信息,只有在调用Wait或Run后才可用。</span>
<span id="Cmd.ProcessState">ProcessState</span> *<a href="os.htm">os</a>.<a href="os.htm#ProcessState">ProcessState</a>
<span class="com">// 内含隐藏或非导出字段</span>
}</pre>
<p>Cmd代表一个正在准备或者在执行中的外部命令。</p>
<h4 id="Command">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#112">Command</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func Command(name <a href="builtin.htm#string">string</a>, arg ...<a href="builtin.htm#string">string</a>) *<a href="#Cmd">Cmd</a></pre>
<p align="left">函数返回一个*Cmd,用于使用给出的参数执行name指定的程序。返回值只设定了Path和Args两个参数。</p>
<p align="left">如果name不含路径分隔符,将使用LookPath获取完整路径;否则直接使用name。参数arg不应包含命令名。</p>
<div class="panel-group">
<div class="panel panel-default" id="example-Command">
<div class="panel-heading" onclick="document.getElementById('ex-Command').style.display = document.getElementById('ex-Command').style.display=='none'?'block':'none';">Example</div>
<div id="ex-Command" class="panel-collapse collapse">
<div class="panel-body">
<pre>
cmd := exec.Command("tr", "a-z", "A-Z")
cmd.Stdin = strings.NewReader("some input")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Printf("in all caps: %q\n", out.String())
</pre>
</div>
</div>
</div>
</div>
<h4 id="Cmd.StdinPipe">func (*Cmd) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#411">StdinPipe</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (c *<a href="#Cmd">Cmd</a>) StdinPipe() (<a href="io.htm">io</a>.<a href="io.htm#WriteCloser">WriteCloser</a>, <a href="builtin.htm#error">error</a>)</pre>
<p>StdinPipe方法返回一个在命令Start后与命令标准输入关联的管道。Wait方法获知命令结束后会关闭这个管道。必要时调用者可以调用Close方法来强行关闭管道,例如命令在输入关闭后才会执行返回时需要显式关闭管道。</p>
<h4 id="Cmd.StdoutPipe">func (*Cmd) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#453">StdoutPipe</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (c *<a href="#Cmd">Cmd</a>) StdoutPipe() (<a href="io.htm">io</a>.<a href="io.htm#ReadCloser">ReadCloser</a>, <a href="builtin.htm#error">error</a>)</pre>
<p>StdoutPipe方法返回一个在命令Start后与命令标准输出关联的管道。Wait方法获知命令结束后会关闭这个管道,一般不需要显式的关闭该管道。但是在从管道读取完全部数据之前调用Wait是错误的;同样使用StdoutPipe方法时调用Run函数也是错误的。参见下例:</p>
<div class="panel-group">
<div class="panel panel-default" id="example-Cmd-StdoutPipe">
<div class="panel-heading" onclick="document.getElementById('ex-Cmd-StdoutPipe').style.display = document.getElementById('ex-Cmd-StdoutPipe').style.display=='none'?'block':'none';">Example</div>
<div id="ex-Cmd-StdoutPipe" class="panel-collapse collapse">
<div class="panel-body">
<pre>
cmd := exec.Command("echo", "-n", `{"Name": "Bob", "Age": 32}`)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
var person struct {
Name string
Age int
}
if err := json.NewDecoder(stdout).Decode(&person); err != nil {
log.Fatal(err)
}
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
fmt.Printf("%s is %d years old\n", person.Name, person.Age)
</pre>
</div>
</div>
</div>
</div>
<h4 id="Cmd.StderrPipe">func (*Cmd) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#478">StderrPipe</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (c *<a href="#Cmd">Cmd</a>) StderrPipe() (<a href="io.htm">io</a>.<a href="io.htm#ReadCloser">ReadCloser</a>, <a href="builtin.htm#error">error</a>)</pre>
<p>StderrPipe方法返回一个在命令Start后与命令标准错误输出关联的管道。Wait方法获知命令结束后会关闭这个管道,一般不需要显式的关闭该管道。但是在从管道读取完全部数据之前调用Wait是错误的;同样使用StderrPipe方法时调用Run函数也是错误的。请参照StdoutPipe的例子。</p>
<h4 id="Cmd.Run">func (*Cmd) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#235">Run</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (c *<a href="#Cmd">Cmd</a>) Run() <a href="builtin.htm#error">error</a></pre>
<p align="left">Run执行c包含的命令,并阻塞直到完成。</p>
<p align="left">如果命令成功执行,stdin、stdout、stderr的转交没有问题,并且返回状态码为0,方法的返回值为nil;如果命令没有执行或者执行失败,会返回*ExitError类型的错误;否则返回的error可能是表示I/O问题。</p>
<h4 id="Cmd.Start">func (*Cmd) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#272">Start</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (c *<a href="#Cmd">Cmd</a>) Start() <a href="builtin.htm#error">error</a></pre>
<p>Start开始执行c包含的命令,但并不会等待该命令完成即返回。Wait方法会返回命令的返回状态码并在命令返回后释放相关的资源。</p>
<div class="panel-group">
<div class="panel panel-default" id="example-Cmd-Start">
<div class="panel-heading" onclick="document.getElementById('ex-Cmd-Start').style.display = document.getElementById('ex-Cmd-Start').style.display=='none'?'block':'none';">Example</div>
<div id="ex-Cmd-Start" class="panel-collapse collapse">
<div class="panel-body">
<pre>
cmd := exec.Command("sleep", "5")
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
log.Printf("Waiting for command to finish...")
err = cmd.Wait()
log.Printf("Command finished with error: %v", err)
</pre>
</div>
</div>
</div>
</div>
<h4 id="Cmd.Wait">func (*Cmd) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#349">Wait</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (c *<a href="#Cmd">Cmd</a>) Wait() <a href="builtin.htm#error">error</a></pre>
<p align="left">Wait会阻塞直到该命令执行完成,该命令必须是被Start方法开始执行的。</p>
<p align="left">如果命令成功执行,stdin、stdout、stderr的转交没有问题,并且返回状态码为0,方法的返回值为nil;如果命令没有执行或者执行失败,会返回*ExitError类型的错误;否则返回的error可能是表示I/O问题。Wait方法会在命令返回后释放相关的资源。</p>
<h4 id="Cmd.Output">func (*Cmd) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#379">Output</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (c *<a href="#Cmd">Cmd</a>) Output() ([]<a href="builtin.htm#byte">byte</a>, <a href="builtin.htm#error">error</a>)</pre>
<p>执行命令并返回标准输出的切片。</p>
<div class="panel-group">
<div class="panel panel-default" id="example-Cmd-Output">
<div class="panel-heading" onclick="document.getElementById('ex-Cmd-Output').style.display = document.getElementById('ex-Cmd-Output').style.display=='none'?'block':'none';">Example</div>
<div id="ex-Cmd-Output" class="panel-collapse collapse">
<div class="panel-body">
<pre>
out, err := exec.Command("date").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("The date is %s\n", out)
</pre>
</div>
</div>
</div>
</div>
<h4 id="Cmd.CombinedOutput">func (*Cmd) <a title="View Source" href="https://github.com/golang/go/blob/master/src/os/exec/exec.go?name=release#391">CombinedOutput</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (c *<a href="#Cmd">Cmd</a>) CombinedOutput() ([]<a href="builtin.htm#byte">byte</a>, <a href="builtin.htm#error">error</a>)</pre>
<p>执行命令并返回标准输出和错误输出合并的切片。</p>
</div>
</body>
</html>