如何在 elisp 中捕获 shell 命令的标准输出?

问题描述 投票:0回答:2

我想在 Emacs 中运行 shell 命令并将完整输出捕获到变量中。有没有办法做到这一点?例如,我希望能够通过以下方式将

hello-string
设置为
"hello"

(setq hello-string (capture-stdout-of-shell-command "/bin/echo hello"))

函数

capture-stdout-of-shell-command
是否存在,如果存在,它的真实名称是什么?

emacs elisp stdout capture
2个回答
80
投票

shell-command-to-string
达到你的目的了吗?

例如:

(shell-command-to-string "/bin/echo hello")

26
投票

我有一个建议可以扩展伊势紫藤的答案。尝试使用这样的东西:

(setq my_shell_output
  (substring 
    (shell-command-to-string "/bin/echo hello") 
   0 -1))

这应该将字符串

"hello"
设置为
my_shell_output
的值,但很干净。使用
(substring)
可以消除 emacs 调用 shell 命令时经常出现的尾随
\n
。它让我在 Windows 上运行的 emacs 中感到困扰,并且可能也会在其他平台上发生。

© www.soinside.com 2019 - 2024. All rights reserved.