2021-02-07 05:19:05 +01:00
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 ( ' / b i n / b a s h ' )
if redirect {
cmd . set_args ( [ ' - c ' , ' / b i n / b a s h / t m p / t e s t . s h 2 > & 1 ' ] )
} else {
cmd . set_args ( [ path ] )
}
cmd . set_redirect_stdio ( )
cmd . run ( )
2023-07-18 16:22:26 +02:00
for cmd . is_alive ( ) {
line = cmd . stdout_read ( )
println ( ' S T D O U T : $ { line } ' )
if ! redirect {
line_err = cmd . stderr_read ( )
println ( ' S T D E R R : $ { line_err } ' )
2021-02-07 05:19:05 +01:00
}
}
if cmd . code > 0 {
println ( ' E R R O R : ' )
println ( cmd )
// println(cmd.stderr_read())
}
}
fn main ( ) {
script := '
e c h o l i n e 1
# w i l l u s e s o m e s t d e r r n o w
e c h o r e d i r e c t 1 t o 2 1 > & 2
e c h o l i n e 3
'
2021-03-01 00:18:14 +01:00
os . write_file ( ' / t m p / t e s t . s h ' , script ) or { panic ( err ) }
2021-02-07 05:19:05 +01:00
// 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 ( ' / t m p / t e s t . s h ' , false )
// this will always work
exec ( ' / t m p / t e s t . s h ' , true )
}