how to exec.Command() long string / multiple commands

3,301 views
Skip to first unread message

RoboTamer

unread,
Oct 1, 2012, 10:48:03 PM10/1/12
to golan...@googlegroups.com

how do I execute following with exec.Command()

git ls-tree -r --name-only HEAD  | while read filename; do echo "$(git log -1 --format="%at" -- $filename) $filename"; done

I tried thinks like:
exec.Command("git ls-tree -r --name-only HEAD  | while read filename; do echo \"$(git log -1 --format=\"%at\" -- $filename) $filename\"; done")
exec.Command("git", "ls-tree -r --name-only HEAD  | while read filename; do echo \"$(git log -1 --format=\"%at\" -- $filename) $filename\"; done")

And more splitting but it doesn't seam to work


Matt Kane's Brain

unread,
Oct 1, 2012, 10:55:04 PM10/1/12
to RoboTamer, golan...@googlegroups.com
exec.Command doesn't do any splitting of strings into individual
command-line arguments. You would need to call strings.Split()
yourself. However, the pipe is going to be a problem as exec.Command
doesn't spawn a shell. You might have better luck with:

exec.Command("bash", "-c", `git ls-tree -r --name-only HEAD | while
read filename; do echo "$(git log
-1 --format="%at" -- $filename) $filename"; done`)

(Didn't test that, uses raw strings instead of escaped quotes)
> --
>
>



--
matt kane's brain
http://hydrogenproject.com

RoboTamer

unread,
Oct 1, 2012, 10:59:31 PM10/1/12
to golan...@googlegroups.com, RoboTamer

On Monday, October 1, 2012 7:55:32 PM UTC-7, mkb wrote:
exec.Command doesn't do any splitting of strings into individual
command-line arguments. You would need to call strings.Split()
yourself. However, the pipe is going to be a problem as exec.Command
doesn't spawn a shell. You might have better luck with:

exec.Command("bash", "-c", `git ls-tree -r --name-only HEAD  | while
read filename; do echo "$(git log
-1 --format="%at" -- $filename) $filename"; done`)

(Didn't test that, uses raw strings instead of escaped quotes)

--
matt kane's brain
http://hydrogenproject.com

Thanks Matt, no bug, works as proposed  
Reply all
Reply to author
Forward
0 new messages