Hi guys,

I'm in need of using the "script" command (more info about script in the 
end) in Linux to run a couple of commands and get the exit status of the 
second command.

*Here is my code snippet:*

package main


import (
 "fmt"
 "os/exec"
)


func main() {
 op, err := exec.Command("script", "-e", "-q", "-c", "date; exit 1").
CombinedOutput()
 if err != nil {
 fmt.Println("Error while running command:", err)
 fmt.Println(string(op))
 return
 }
 fmt.Println("Successfully ran the command.")
 fmt.Println(string(op))
}

When I compile this code and run it on two Red Hat VMs, they give me 
different results.

*Working as expected in a Red Hat 8 VM:*
[root@dc-rhel8-1 tmp]# ./script_command 
Error while running command: exit status 1
Wed Jan 29 21:41:20 IST 2020


[root@dc-rhel8-1 tmp]# script -e -q -c "date; exit 1"
Wed Jan 29 21:42:11 IST 2020
[root@dc-rhel8-1 tmp]# echo $?
1
[root@dc-rhel8-1 tmp]# script --version
script from util-linux 2.32.1

*Incorrect working in a Red Hat 7 VM:*
[root@dc-rhel7-dev tmp]# ./script_command 
Successfully ran the command.
Wed Jan 29 21:41:41 IST 2020


[root@dc-rhel7-dev tmp]# script -e -q -c "date; exit 1"
Wed Jan 29 21:42:22 IST 2020
[root@dc-rhel7-dev tmp]# echo $?
1
[root@dc-rhel7-dev tmp]# script --version
script from util-linux 2.23.2

Is this behaviour an issue in Go? Or am I missing something?

Thanks in advance!

*Some information on the script command:*
By default, when I pipe output from RunCommand, the default terminal width 
is 80, due to which the output will be scrambled and hard to parse.
So, I'm using the script command and stty to set the width to a bigger 
value.
-e is used to return the exit status of the child process
-q is used to keep output minimal
-c is used to specify the command that is to be run

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c27ec083-9335-4e0c-bc2d-8c17bf80afb6%40googlegroups.com.

Reply via email to