Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Emacs 如何在elisp中捕获shell命令的标准输出?_Emacs_Elisp_Stdout_Capture - Fatal编程技术网

Emacs 如何在elisp中捕获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

我想在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-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”)