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)
return
}
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.Run()
cmd = exec.Command("/bin/bash", "-c", "mount /dev/", input, "p1 /mnt")
cmd.Run()
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.
CodePudding user response:
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.
CodePudding user response:
Thanks everyone, the code is working correctly in this way.
input = strings.TrimSuffix(input, "\n")
mount := fmt.Sprintf(`qemu-nbd -c /dev/%s /tmp/ios.qcow2`, input)
cmd = exec.Command("bash", "-c", mount)
cmd.Run()
mount1 := fmt.Sprintf(`mount /dev/%sp1 /mnt`, input)
cmd = exec.Command("bash", "-c", mount1)
cmd.Run()