Jor*_*ndo 14
shell-command-to-string
只是围绕更基本的流程功能的便利包装.
用于简单同步过程的良好功能是call-process
.调用进程将从进程返回退出代码,您可以将所有输出重定向到可用于buffer-string
获取文本的缓冲区.
这是一个例子:
;; this single expression returns a list of two elements, the process
;; exit code, and the process output
(with-temp-buffer
(list (call-process "ls" nil (current-buffer) nil "-h" "-l")
(buffer-string)))
;; we could wrap it up nicely:
(defun process-exit-code-and-output (program &rest args)
"Run PROGRAM with ARGS and return the exit code and output in a list."
(with-temp-buffer
(list (apply 'call-process program nil (current-buffer) nil args)
(buffer-string))))
(process-exit-code-and-output "ls" "-h" "-l" "-a") ;; => (0 "-r-w-r-- 1 ...")
Run Code Online (Sandbox Code Playgroud)
另一个注意事项:如果你最终想要对进程做更复杂的事情,你应该阅读文档start-process
,以及如何使用sentinals和过滤器,它真的是一个强大的API.