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

发布于 2024-10-17 12:35:47 字数 293 浏览 5 评论 0原文

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

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

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

I want to run a shell command within Emacs and capture the full output to a variable. Is there a way to do this? For example, I would like to be able to set hello-string to "hello" in the following manner:

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

Does the function capture-stdout-of-shell-command exist, and if so what is its real name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

依 靠 2024-10-24 12:35:47

shell-command- to-string 满足您的目的吗?

例如:

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

Does shell-command-to-string meet your purpose?

For example:

(shell-command-to-string "/bin/echo hello")
┊风居住的梦幻卍 2024-10-24 12:35:47

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

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

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

I have a suggestion to made that extends Ise Wisteria's answer. Try using something like this:

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

This should set the string "hello" as the value of my_shell_output, but cleanly. Using (substring) eliminates the trailing \n that tends to occur when emacs calls out to a shell command. It bothers me in emacs running on Windows, and probably occurs on other platforms as well.

探春 2024-10-24 12:35:47

最后,“git log”“--reverse”选项派上了用场)。使用最初始提交时的当前缓冲区文件打开缓冲区。

(vc-revision-other-window (car (process-lines "git" "log" "--format=%h" "--reverse")))

而关于这个问题

(setq hello-string (car (process-lines "echo" "hello")))

Finally, the "git log" "--reverse" option came in handy ). Open a buffer with the current buffer file as of the most initial commit.

(vc-revision-other-window (car (process-lines "git" "log" "--format=%h" "--reverse")))

And on the question of

(setq hello-string (car (process-lines "echo" "hello")))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文