0

I am trying to run multiple commands using bash -c in exec.Command, when i manually run the command, it returns no error the output of command is nil but it's okay. I don't know why it returns exit status 1 when i run it through golang exec.Command.

Here is my code :

cmd := exec.Command("bash", "-c", "blkid -o device | grep -v part | grep /dev/mapper")
_ = cmd.Wait()
stdout, err := cmd.Output()

I even tried this answer from another question, but it also didn't worked out. The important thing is that when i run command on a vm where output is not nil the command executes successfully, but when i tried this on Centos where output is nil it failed.

1
  • @Inian i need to run command in one line, and have to keep the code simple Commented Jun 3, 2021 at 8:02

1 Answer 1

1

This is because you have to pass the command in the string slice. Use this instead -

cmd := exec.Command("bash",strings.Fields("-c blkid -o device | grep -v part | grep /dev/mapper"))
stdout, err := cmd.Output()
Sign up to request clarification or add additional context in comments.

2 Comments

cannot use type [] string as type string
@KashifKhan0771 Sorry my bad, I have updated my answer. Please have a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.