[comp.emacs] lisp func to return output of shell command

allen@NERD.SSC.GOV (Mike Allen) (03/01/91)

>>>>> On 20 Feb 91 21:11:40 GMT, samsung!crackers!m2c!umvlsi!umvlsi.ecs.umass.edu@uunet.uu.net  (Liam Breck) said:
Liam> Resent-Date: 20 Feb 91 21:11:40 GMT
Liam> Resent-From: gnulists@ai.mit.edu

Liam> Hi all...

Liam> You know the handy system("shell_command_here") call in C that returns
Liam> the command's output as a string?  Well I need something like that for
Liam> Emacs Lisp...  An Emacs function that will execute a a shell command
Liam> and return it's output (a short string) which will be bound to a
Liam> variable.

Liam> I have tried to find such a thing in Info and by Apropos with every
Liam> keyword I could think of (shell, process, exec, system, call, command,
Liam> etc.) so if I've missed something, I apologize, but please don't tell
Liam> me to RTFM.
I suggest you get the lisp manual, there is a whole chapter on using
processes from emacs lisp.

Here is an excerpt that discusses what you want to do (I think):

 * Function: process-filter PROCESS

     This function returns the filter function of PROCESS, or `nil'
     if it has none.

  Here is an example of use of a filter function:

     (defun keep-output (process output)
        (setq kept (cons output kept)))
          => keep-output
     (setq kept nil)
          => nil
     (set-process-filter (get-process "shell") 'keep-output)
          => keep-output
     (process-send-string "shell" "ls ~/other\n")
          => nil
     kept
          => ("lewis@slug[8] % "
     "FINAL-W87-SHORT.MSS    backup.otl              kolstad.mss~
     address.txt             backup.psf              kolstad.psf
     backup.bib~             david.mss               resume-Dec-86.mss~
     backup.err              david.psf               resume-Dec.psf
     backup.mss              dland                   syllabus.mss
     "
     "#backups.mss#          backup.mss~             kolstad.mss
     ")

Here is another, more realistic example, which demonstrates how to
use the process mark to do insertion in the same fashion as is done
when there is no filter function:

     ;; Insert input in the buffer specified by `my-shell-buffer'
     ;; and make sure that buffer is shown in some window.
     (defun my-process-filter (proc str)
         (let ((cur (selected-window))
               (pop-up-windows t))
           (pop-to-buffer my-shell-buffer)
           (goto-char (point-max))
           (insert str)
           (set-marker (process-mark proc) (point-max))
           (select-window cur)))

aks@ANYWHERE.UCSB.EDU (Alan Stebbens) (03/02/91)

> You know the handy system("shell_command_here") call in C that returns
> the command's output as a string?  Well I need something like that for
> Emacs Lisp...  An Emacs function that will execute a a shell command
> and return it's output (a short string) which will be bound to a
> variable.

Try "C-u M-x shell-command-on-region" with the current region being the
input, and it will be replaced by the output.

Or, you could try the following functions (which haven't been thoroughly
tested..)

(defun shell-command-to-string (command)
  "Execute string COMMAND in inferior shell; output is returned as a string."
  (interactive "sShell command: ")
  (shell-command-on-region-to-string (point) (point) command))

(defun shell-command-on-region-to-string (start end command)
  "Execute string COMMAND on current region in inferior shell; output is 
returned as a string."
  (interactive "r\nsShell command: ")
  (let ((buffer (get-buffer-create " *temp Shell Command Output*"))
	str)
    (save-excursion
      (set-buffer buffer)
      (erase-buffer)
      (call-process-region start end shell-file-name
			   nil buffer nil
			   "-c" command)
      (setq str (buffer-string))
      (kill-buffer buffer))
    str))

Then, to use do, for example:

	(setq results (shell-command-to-string "ps aux"))

Alan