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 --clipboard

One solution is to use exec.Command() to run

    sh -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 <lgod...@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 golang-nuts+unsubscr...@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
> <https://groups.google.com/d/msgid/golang-nuts/f9b34d9d-985f-4cbf-9a92-d3d46d72cf55%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
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/CABx2%3DD-Wtx-w0A%3DtJ-16Hmj%3DEqnbQLggDE06cBJRPUU2fLna%3Dg%40mail.gmail.com.

Reply via email to