--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f9b34d9d-985f-4cbf-9a92-d3d46d72cf55%40googlegroups.com.
The exec.Command() function works fine. The problem lies in your understanding of what happens when you use a shell to execute that command versus what the Go function does. When you type that command at a shell prompt it parses the statement and creates a pipe and runs "echo" on the LHS and "xsel" on the RHS of the pipe. The exec.Command() function does none of that work. It simply runs an external program named "echo" with the two arguments you provided. In other words it is as if you typed the following at a shell prompt:echo "echo 'ABC'" "|xsel --clipboard"Resulting in the expected output given the arguments you passed to that function:echo 'ABC' |xsel --clipboardOne solution is to use exec.Command() to runsh -c "echo 'ABC' |xsel --clipboard"That, however, is difficult to do in the general case since getting the quoting correct is a challenge.
On Wed, Mar 25, 2020 at 11:30 AM <lgo...@gmail.com> wrote:
This simple command works correctly when submitted via the terminal, but the same command submitted to os.exec does not !!--
.. and I can`t understand why...
import ( "fmt"; "os"; "os/exec"; )
func main() {
cb := "ABCD"
cmd := exec.Command ("echo", "echo "+ "'" +string(cb [0:len(cb)-1] ) +"'", " |xsel --clipboard" )
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run() ; if err != nil { fmt.Printf("\ncmd.Run() error: %s \n", err) }
}
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 golan...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f9b34d9d-985f-4cbf-9a92-d3d46d72cf55%40googlegroups.com.