-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
Comments
go version: 1.4.1 linux/amd64 |
The > is a feature of your shell language. To use it, you need to run your command inside a shell. Perhaps something like
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. |
just talk about 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 why and i wonder why |
@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. |
all right,thanks. |
when an using exec.Cmd to fock an subprocess,with redirection like cat xxx > /tmp/xxxx ,it will always fail.
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
The text was updated successfully, but these errors were encountered: