Does os.exec work ?

76 views
Skip to first unread message

lgo...@gmail.com

unread,
Mar 25, 2020, 2:30:45 PM3/25/20
to golang-nuts
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) }

}

Jan Mercl

unread,
Mar 25, 2020, 2:52:58 PM3/25/20
to lgo...@gmail.com, golang-nuts
On Wed, Mar 25, 2020 at 7:30 PM <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...

os.Exec does not pass commands to the shell, but it is the case in the
terminal. Try something like this (not tested)

cmd := exec.Command("sh", "-c", "echo foo bar | tee test") // Just an
example, this is not fixing your code.

Kurtis Rader

unread,
Mar 25, 2020, 2:54:07 PM3/25/20
to lgo...@gmail.com, golang-nuts
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.


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


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

lgo...@gmail.com

unread,
Mar 25, 2020, 3:33:22 PM3/25/20
to golang-nuts
Than you..
I surely need to beef-up my comprehension of what os.exec does vs what it does not.


On Wednesday, March 25, 2020 at 2:54:07 PM UTC-4, Kurtis Rader wrote:
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 <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.

howar...@gmail.com

unread,
Mar 25, 2020, 3:41:42 PM3/25/20
to golang-nuts
What you are really wanting there is to run xsel --clipboard and pipe data into it. Talk to your cmd.Stdin!
This is mostly straight from the StdinPipe example in the go docs:

cmd := exec.Command ("xsel","--clipboard" )
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}

go func() {
defer stdin.Close()
io.WriteString(stdin, "values written to stdin are passed to cmd's standard input and in this case end up on the X clipboard")
}()

out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
Reply all
Reply to author
Forward
0 new messages