0

I have a series of system calls using this syntax:

(call-process "/bin/sh" nil nil t "-c" command)

where command is the shell command. I wonder if there is a way to print the output when I have these system calls in an commands.el file and I call it with emacs --script commands.el. Currently, the commands are executed but all of the output appears to be captured and not printed.

3
  • The output is normally collected in the current buffer, but you have specified to have it discarded by passing nil as the third argument. You can specify a different buffer through this parameter, too. Commented Aug 13, 2012 at 9:27
  • Do I have to capture it in a temporary buffer and then print the buffer? I tried t for the third argument but it doesn't get printed either (not sure what "current buffer" would be when ran as a script). Commented Aug 13, 2012 at 10:12
  • That sounds like a workable solution, yes. If your session doesn't set up a buffer at any point, I'm guessing the current buffer would end up being the *scratch* buffer. (Update: Confirmed.) That one contains some comments so it's probably better to create a new, empty buffer when you start. Commented Aug 13, 2012 at 10:52

2 Answers 2

1

It's probably easier to let emacs manage the temp buffer for you:

(defun run-it (cmd)
  "run CMD in shell and return result as string."
  (with-temp-buffer
    (call-process "/bin/sh" nil t t "-c" cmd)
    (buffer-string)))
Sign up to request clarification or add additional context in comments.

1 Comment

If you want to run a shell command like this, just use the existing function shell-command-to-string. The code is almost identical (look in simple.el), but automatically uses the right shell etc.
1

Something like this, then?

(set-buffer (get-buffer-create " output"))
(mapc (function (lambda (cmd) (call-process "/bin/sh" nil t t "-c" cmd)))
  '("echo hello"
    "perl -e \"die qq(goodbye)\"") )
(message "%s" (buffer-string))

This outputs at -e line 1. at the end of output. Maybe add an empty line before it, or figure out a way to suppress it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.