Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os, os/exec: using redirection symbol '<' '>' failed #11866

Closed
liugangnhm opened this issue Jul 25, 2015 · 5 comments
Closed

os, os/exec: using redirection symbol '<' '>' failed #11866

liugangnhm opened this issue Jul 25, 2015 · 5 comments

Comments

@liugangnhm
Copy link

when an using exec.Cmd to fock an subprocess,with redirection like cat xxx > /tmp/xxxx ,it will always fail.

cmd:=exec.Command("cat","./aaa",">","/tmp/aaa")
cmd.Start()
err:=cmd.Wait()
if err!=nil {
    t.Errorf("exit error %s", err)
}

wheather /tmp/aaa is exist or not, error is always "exit status 1" .
file aaa is always exist.

when i set cmd's Stderr to bytes.Buffer ,and i see detail error:"cat: >d: no such file or directory". so i think go maybe treat '>' as a file name.

so how should i do when i want to use redirection symbol in command

@liugangnhm
Copy link
Author

go version: 1.4.1 linux/amd64
os version:ubuntu 14.10 amd64

@crawshaw
Copy link
Member

The > is a feature of your shell language. To use it, you need to run your command inside a shell. Perhaps something like

cmd := exec.Command("bash", "-c", "echo hello > /tmp/aaa")

Be careful with this though. If you construct a string and pass it to a shell, you need to think carefully about escaping and security. For the particular example you give, you may want to consider using os.Open, os.Create, and io.Copy.

@mikioh mikioh changed the title using redirection symbol '<' '>' failed os, os/exec: using redirection symbol '<' '>' failed Jul 26, 2015
@liugangnhm
Copy link
Author

just talk about cat

package main

import (
    "bytes"
    "os/exec"
    "testing"
)

func TestExe1(t *testing.T) {

    out := &bytes.Buffer{}

    cmd := exec.Command("bash", "-c", "cat ./file_not_exist > /tmp/aaa")
    cmd.Stderr = out
    err := cmd.Start()
    if err != nil {
        t.Errorf("start error %s [%s]", err, out.String())
    }
    err = cmd.Wait()
    if err == nil {
        t.Errorf("exit error %s [%s]", err, out.String())
    }

    cmd = exec.Command("bash", "-c", "echo hello > /tmp/aaa")
    err = cmd.Start()
    if err != nil {
        t.Errorf("start error %s [%s]", err, out.String())
    }
    err = cmd.Wait()
    if err == nil {
        t.Errorf("exit error %s [%s]", err, out.String())
    }

    cmd = exec.Command("bash", "-c", "cat ./myself_test.go > /tmp/aaa")
    err = cmd.Start()
    if err != nil {
        t.Errorf("start error %s [%s]", err, out.String())
    }
    err = cmd.Wait()
    if err != nil {
        t.Errorf("exit error %s [%s]", err, out.String())
    }
}

output is
golang_test.go:41: exit error exit status 1 [bash: /tmp/aaa: Permission denied

why cat is particular?

and i wonder why cmd := exec.Command("bash", "-c", "cat ./file_not_exist > /tmp/aaa") did not throw an error.

@ianlancetaylor
Copy link
Contributor

@liugangnhm We prefer to use the issue tracker for bug reports. Please ask questions on the golang-nuts mailing list, not on the issue tracker. Thanks.

@liugangnhm
Copy link
Author

all right,thanks.

@golang golang locked and limited conversation to collaborators Aug 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants