Emacs 如何在elisp中捕获shell命令的标准输出?
我想在Emacs中运行shell命令,并捕获变量的完整输出。有办法做到这一点吗?例如,我希望能够通过以下方式将Emacs 如何在elisp中捕获shell命令的标准输出?,emacs,elisp,stdout,capture,Emacs,Elisp,Stdout,Capture,我想在Emacs中运行shell命令,并捕获变量的完整输出。有办法做到这一点吗?例如,我希望能够通过以下方式将hello string设置为“hello”: (setq hello-string (capture-stdout-of-shell-command "/bin/echo hello")) 函数capture stdout of shell command是否存在,如果存在,它的真实名称是什么?shell command to string是否符合您的目的? 例如: (shell-c
hello string
设置为“hello”
:
(setq hello-string (capture-stdout-of-shell-command "/bin/echo hello"))
函数
capture stdout of shell command
是否存在,如果存在,它的真实名称是什么?shell command to string是否符合您的目的?例如:
(shell-command-to-string "/bin/echo hello")
希望这能有所帮助。我有一个建议可以扩展Ise Wisteria的答案。尝试使用类似以下内容:
(setq my_shell_output
(substring
(shell-command-to-string "/bin/echo hello")
0 -1))
这应该将字符串
“hello”
设置为my\u shell\u output
的值,但要干净。使用(子字符串)
可以消除在emacs调用shell命令时出现的尾部\n
。在Windows上运行的emacs中,这让我感到困扰,而且可能也发生在其他平台上 我注意到emacs在Mac OS X和Linux上都这样做,所以它不仅仅是NTEmacs。(将我的shell命令定义为string(&rest cmd)(将regexp替换为string“\r?\n$”(shell命令定义为string(mapcont'identity cmd”))
@spacebat您想要\\'
,not$
echo hello
是hello\n
。在linux/osx中,您可以使用-n
停止输出尾随换行符,例如echo-n hello
注意shell命令到string
捕获stdout
和stderr
。幸运的是,我们可以通过管道将stderr连接到/dev/null:(shell命令到string)/bin/echo hello 2>/dev/null”)