It is possible to pass the value of user input into bash's command?


    fmt.Print("Enter valid nbd: ")
    reader := bufio.NewReader(os.Stdin)

    input, err := reader.ReadString('\n')
    if err != nil {
            fmt.Println("An error occured while reading input. Please try again", err)

    input = strings.TrimSuffix(input, "\n")
    cmd = exec.Command("/bin/bash", "-c", "qemu-nbd -c /dev/", input, "/tmp/var/lib/vz/images/201/vm-201-disk-0.qcow2")

    cmd = exec.Command("/bin/bash", "-c", "mount /dev/", input, "p1 /mnt")

I want to pass user input, for example nbd7, to both exec.Command as I mentioned.

input = strings.TrimSuffix(input, "\n")

mount := qemu-nbd -c /dev/input /tmp/CentOS-7.7.1908-x64.qcow2

cmd = exec.Command("/bin/bash", "-c", mount, "echo stdout; echo 1>&2 stderr")

I have modified abit of my code. Any proper way that I can pass my input value into mount variable’s value? /dev/input definitely not working.


It is possible. Code is correct, you can use cmd.String to print executed command. There is most likely error during execution.

I would recommend to use cmd.Output() and cmd.StderrPipe() for debugging.

// Output runs the command and returns its standard output.
// Any returned error will usually be of type *ExitError.
// If c.Stderr was nil, Output populates ExitError.Stderr.

Answered By – medasx

Answer Checked By – Willingham (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.