如何使用Golang运行内置的Shell命令

huangapple go评论87阅读模式
英文:

How to run a built in shell command using golang

问题

我正在尝试在Golang中运行一个内置的shell命令并获取结果。

以下是代码:

package main

import (
	"fmt"
	"os/exec"
)

func main(){
	cmd := exec.Command("sh", "-c", "history")
	stdout, err := cmd.Output()
	
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Println(string(stdout))
}

然而,我只得到输出:

exit status 127

任何帮助将不胜感激。

英文:

I am trying to run a built in shell command in golang and get the result.
Here is the code:

package main

import (
	"fmt"
	"os/exec"
)

func main(){
	cmd := exec.Command("sh", "-c", "history")
	stdout, err := cmd.Output()
	
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Println(string(stdout))
}

However i just get the output:

exit status 127

Any help would be appreciated.

答案1

得分: 2

"bash", "-c" 创建一个非交互式的 bash,接下来的命令将在非交互式环境下运行。根据我的研究,非交互式 shell 不支持启用历史记录。这就是为什么仅使用 "bash", "-c", "history" 命令时,输出为空的原因。

我找到的解决方法如下:

package main

import (
    "fmt"
    "os/exec"
)

func main(){
    cmd := exec.Command("bash", "-c", "history -r ~/.bash_history; history")
    stdout, err := cmd.Output()
    
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Println(string(stdout))
}

如果你使用的是其他 shell,可能需要将 bash~/.bash_history 替换为相应的值。我不确定 :/

详细信息:
如何在给定 HISTFILE 路径的非交互式 shell 中查看 history 的输出?
bash -c 和非交互式 shell
为什么在脚本文件中 history 命令无效?

如果我提供的任何信息有误,请告诉我。我根据我在“详细信息”下发布的网站内容给出了这个答案。

英文:

"bash", "-c" creates a bash that is non-interactive and its next commands will run assuming non-interactive. And according to my researches history is not enabled for non-interactive shells. That is why history command alone outputs nothing with "bash", "-c", "history"
The workaround that I found:

package main

import (
    "fmt"
    "os/exec"
)

func main(){
    cmd := exec.Command("bash", "-c", "history -r ~/.bash_history; history")
    stdout, err := cmd.Output()
    
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Println(string(stdout))
}

Maybe you need replace the bash and ~/.bash_history with corresponding values that is related with another shell, if you are using another shell. I don't know :/

Details:
How to view the output of history from a non-interactive shell given path to HISTFILE?
bash -c and noninteractive shell
Why does the history command do nothing in a script file?

If my given any information is wrong please notify me. I have made this answer according to the sites that I posted under Details.

答案2

得分: 1

问题在于 sh -c history 不起作用。你启动的 shell 没有该命令。history 不是一个标准的 POSIX 命令。

你可能在一个 /bin/sh 恰好是 Dash 的系统上。

如果 sh 恰好是 Bash,history 命令将成功执行,即使 Bash 被作为 sh 调用;Bash 在 POSIX 模式下不隐藏该命令。

在我这里的一个系统上:

$ sh -c history
sh: 1: history: not found
$ dash -c history
dash: 1: history: not found
$ bash -c history
$ bash --posix -c history
$ bash --posix -c history
$ ln -sf /bin/bash ./sh
$ ./sh -c history
$ rm sh
英文:

The problem is that sh -c history doesn't work. The shell you're launching doesn't have that command. history is not a standard POSIX command.

You might be on a system where /bin/sh happens to be Dash.

If sh happens to be Bash, the history command will successfully execute, even if Bash is invoked as sh; Bash doesn't hide that command in POSIX mode.

On a system I have here:

$ sh -c history
sh: 1: history: not found
$ dash -c history
dash: 1: history: not found
$ bash -c history
$ bash --posix -c history
$ bash --posix -c history
$ ln -sf /bin/bash ./sh
$ ./sh -c history
$ rm sh

huangapple
  • 本文由 发表于 2023年7月2日 04:26:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76596444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定