On Wed, Apr 20, 2022 at 6:16 PM Dean Schulze <dean.w.schu...@gmail.com>
wrote:

> I need to execute this tar command
>
> *tar xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/*
>
> from within a Go program.  I've verified that it works from the command
> line.  I've tried using
>
>
> *argStr := "xzf dir1/dir2/somefile.tgz --directory=dir1/dir2/"output, err
> := exec.Command("tar", argStr).Output()*
>

You need to pass the args separately, not as one string:
output, err := exec.Command("tar", "xzf", "dir1/dir2/somefile.tgz",
"--directory=dir1/dir2/").Output()
I also tend to set `cmd.Stderr = os.Stderr` to make sure I see error
messages, when executing commands.


>
>
> but it returns an error code of 2 with no other output indicating what is
> wrong.
>
> Using exec.Command(name, arg) is always a guessing game as to what the
> command name should be and what the argument should be.  How do I execute
> this tar command from within Go?
>
> --
> 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/4c4345b7-2155-4bd9-ba61-7a76b5256d2fn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/4c4345b7-2155-4bd9-ba61-7a76b5256d2fn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAEkBMfFC_oZz%2BWxu7b4wuaY0GqchK5wUaLVu6AhQ708OmCjOtw%40mail.gmail.com.

Reply via email to