Go exec.Command 似乎无法将信息通过管道传输或附加到 .txt 文件。我的语法错了吗?
Go exec.Command can't seem to pipe or append information to a .txt file. Is my syntax wrong?
我一直在尝试 运行 一个不必使用 bash 脚本的命令,而是使用 go for 运行ning 命令。
主页=/home/gogopowerrangers
在 ubuntu 18.04
const systemInfoLoc string = "$HOME/systemvar.txt"
//I've tried to run with full path as well here
getSystemKernel := exec.Command("uname", "-s", ">>", "home/username/system.txt")
getSystemKernel.Run()
//^doesn't seem to return anything
//or
getsystemvar,_ = exec.Command("echo", "$USER", ">", "$HOME/systemvar.txt").Output
fmt.Println(string(getsystemvar))
//here even after the system doesn't seen to recognize $USER or $HOME or actually get the variables
我的语法有误吗?我可以将参数作为 args []string 传递,但这似乎没有什么不同。
我知道如果我将这些写入它们工作的 CMD 行或 bash 脚本中。
如果您发现任何错误,请告诉我。
exec.Command()
不会 运行 在 shell 中,它只是 运行 命令。这对您来说意味着它 不会 扩展环境变量 ($HOME
) 或进行重定向 (>
, >>
),因为那是shell 的工作。
除非绝对必要,否则我不建议 运行在 exec.Command()
中使用 shell,因为所需的不受信任输入的引用可能很棘手,并且很可能出现错误(包括安全问题)。
您可以使用 os.ExpandEnv()
or os.GetEnv()
to load the environment variables. You can do redirection by reading the output and then writing it to the desired file, using e.g. ioutil.WriteFile()
.
将它们放在一起看起来像(未经测试):
// Expand the $HOME variable.
systemInfoLoc := os.ExpandEnv("$HOME/systemvar.txt")
// Run uname command and get both stdout and stderr
getSystemKernel, err := exec.Command("uname", "-s").CombinedOutput()
if err != nil {
// Show error and output
log.Fatalf("%s: %s", err, getSystemKernel)
}
// Write result to file
err = ioutil.WriteFile("home/username/system.txt", getSystemKernel, 0644)
if err != nil {
log.Fatal(err)
}
间接是 shell 的一项功能。如果你想使用间接,你有两个选择之一:
运行 通过 shell.
getSystemKernel := exec.Command("/bin/sh", "-c", "uname -s >> home/username/system.txt")
使用os/exec
包,自己处理程序的输出流。
我一直在尝试 运行 一个不必使用 bash 脚本的命令,而是使用 go for 运行ning 命令。 主页=/home/gogopowerrangers 在 ubuntu 18.04
const systemInfoLoc string = "$HOME/systemvar.txt"
//I've tried to run with full path as well here
getSystemKernel := exec.Command("uname", "-s", ">>", "home/username/system.txt")
getSystemKernel.Run()
//^doesn't seem to return anything
//or
getsystemvar,_ = exec.Command("echo", "$USER", ">", "$HOME/systemvar.txt").Output
fmt.Println(string(getsystemvar))
//here even after the system doesn't seen to recognize $USER or $HOME or actually get the variables
我的语法有误吗?我可以将参数作为 args []string 传递,但这似乎没有什么不同。 我知道如果我将这些写入它们工作的 CMD 行或 bash 脚本中。 如果您发现任何错误,请告诉我。
exec.Command()
不会 运行 在 shell 中,它只是 运行 命令。这对您来说意味着它 不会 扩展环境变量 ($HOME
) 或进行重定向 (>
, >>
),因为那是shell 的工作。
除非绝对必要,否则我不建议 运行在 exec.Command()
中使用 shell,因为所需的不受信任输入的引用可能很棘手,并且很可能出现错误(包括安全问题)。
您可以使用 os.ExpandEnv()
or os.GetEnv()
to load the environment variables. You can do redirection by reading the output and then writing it to the desired file, using e.g. ioutil.WriteFile()
.
将它们放在一起看起来像(未经测试):
// Expand the $HOME variable.
systemInfoLoc := os.ExpandEnv("$HOME/systemvar.txt")
// Run uname command and get both stdout and stderr
getSystemKernel, err := exec.Command("uname", "-s").CombinedOutput()
if err != nil {
// Show error and output
log.Fatalf("%s: %s", err, getSystemKernel)
}
// Write result to file
err = ioutil.WriteFile("home/username/system.txt", getSystemKernel, 0644)
if err != nil {
log.Fatal(err)
}
间接是 shell 的一项功能。如果你想使用间接,你有两个选择之一:
运行 通过 shell.
getSystemKernel := exec.Command("/bin/sh", "-c", "uname -s >> home/username/system.txt")
使用
os/exec
包,自己处理程序的输出流。