blob: fa415e78392b6530633d6922f92f968232a619be (
plain)
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
|
module main
import os
// a test where we execute a bash script but work around where we put script in bash inside bash
fn exec(path string, redirect bool) {
mut line := ''
mut line_err := ''
mut cmd := os.new_process('/bin/bash')
if redirect {
cmd.set_args(['-c', '/bin/bash /tmp/test.sh 2>&1'])
} else {
cmd.set_args([path])
}
cmd.set_redirect_stdio()
cmd.run()
if cmd.is_alive() {
for {
line = cmd.stdout_read()
println('STDOUT: $line')
if !redirect {
line_err = cmd.stderr_read()
println('STDERR: $line_err')
}
if !cmd.is_alive() {
break
}
}
}
if cmd.code > 0 {
println('ERROR:')
println(cmd)
// println(cmd.stderr_read())
}
}
fn main() {
script := '
echo line 1
#will use some stderr now
echo redirect 1 to 2 1>&2
echo line 3
'
os.write_file('/tmp/test.sh', script) or { panic(err) }
// os.chmod("/tmp/test.sh",0o700) //make executable
// this will work because stderr/stdout are smaller than 4096 chars, once larger there can be deadlocks
// in other words this can never work reliably without being able to check if there is data on stderr or stdout
exec('/tmp/test.sh', false)
// this will always work
exec('/tmp/test.sh', true)
}
|