exec.Command: How to execute commands with path as argument?

3,256 views
Skip to first unread message

Constantine Vasil

unread,
Oct 2, 2013, 1:29:59 PM10/2/13
to golan...@googlegroups.com
I need to execute this command: 
sv s /etc/service/*

In exec.Command it is:
cmd1 := exec.Command("sv", "s" , "/etc/service/*")
out, err := cmd1.Output()

But I get an error message:
fail: /etc/service/*: unable to change to service directory: file does not exist

How to execute commands with path as argument?

Ian Lance Taylor

unread,
Oct 2, 2013, 1:39:42 PM10/2/13
to Constantine Vasil, golang-nuts
On Unix systems, when you type a command like "sv s /etc/service/*"
into the shell, it is the shell that expands the *. To do this from
Go you will need to either expand the * yourself using filepath.Glob,
or you will need to do something like (untested)
exec.Command("/bin/sh", "-c", "sv s /etc/service/*")

Ian

Constantine Vasil

unread,
Oct 2, 2013, 1:54:01 PM10/2/13
to golan...@googlegroups.com, Constantine Vasil
I found this easier:

created exec.bash  
========================
#exec.bash
========================
exec $1 $2 $3 $4 $5 $6 $7 $8 $9
========================

in Go program:
========================
script := "sv s /etc/service/*"
cmd1 := exec.Command("sudo", runPath+"./exec.bash", script)
========================

This is working but cannot handle pipes.

--Constantine

Kyle Lemons

unread,
Oct 2, 2013, 4:16:43 PM10/2/13
to Constantine Vasil, golang-nuts
On Wed, Oct 2, 2013 at 10:54 AM, Constantine Vasil <ths...@gmail.com> wrote:
I found this easier:

created exec.bash  
========================
#exec.bash
========================
exec $1 $2 $3 $4 $5 $6 $7 $8 $9
========================

I think you want "$@"
 
in Go program:
========================
script := "sv s /etc/service/*"
cmd1 := exec.Command("sudo", runPath+"./exec.bash", script)
========================

but the less hackish way is to do it like this:
exec.Command("sudo", "bash", "-c", "your full command including spaces here")
 
This is working but cannot handle pipes.

--Constantine


On Wednesday, October 2, 2013 10:39:42 AM UTC-7, Ian Lance Taylor wrote:
On Wed, Oct 2, 2013 at 10:29 AM, Constantine Vasil <ths...@gmail.com> wrote:
> I need to execute this command:
> sv s /etc/service/*
>
> In exec.Command it is:
> cmd1 := exec.Command("sv", "s" , "/etc/service/*")
> out, err := cmd1.Output()
>
> But I get an error message:
> fail: /etc/service/*: unable to change to service directory: file does not
> exist
>
> How to execute commands with path as argument?

On Unix systems, when you type a command like "sv s /etc/service/*"
into the shell, it is the shell that expands the *.  To do this from
Go you will need to either expand the * yourself using filepath.Glob,
or you will need to do something like (untested)
    exec.Command("/bin/sh", "-c", "sv s /etc/service/*")

Ian

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Constantine Vasil

unread,
Oct 2, 2013, 6:29:46 PM10/2/13
to golan...@googlegroups.com, Constantine Vasil
This worked and is the simplest way:
exec.Command("sudo", "bash", "-c", "your full command including spaces here")

How you would perform a sequence of commands?

Like this:

1) pwd
2) <current directory>
3) cd /mydir
4) hg pull
5) <enter your password here>
6) cd <current directory>

Jens Alfke

unread,
Oct 2, 2013, 8:02:04 PM10/2/13
to golan...@googlegroups.com, Constantine Vasil


On Wednesday, October 2, 2013 1:16:43 PM UTC-7, Kyle Lemons wrote:
but the less hackish way is to do it like this:
exec.Command("sudo", "bash", "-c", "your full command including spaces here")

This is still hackish, because you now have to deal with escaping/quoting your actual parameters to assemble the shell command line, so that the shell will parse them into the parameters you really wanted. Otherwise you've created one of those classic bugs that could under the right circumstances become an attack vector ... especially since it's being run with root access (i.e. someone submits a thingy named "foo ; rm -rf /" and you just stick that in your command line without adequately quoting it.)

(A somewhat less serious problem is that lack of quoting can also cause scripts to break when a filesystem path includes spaces or other shell metacharacters.)

If you just need to expand a wildcard in a path, use filepath.Glob. Don't spin up a shell just for that.

--Jens
Reply all
Reply to author
Forward
Message has been deleted
0 new messages