You are not logged in.
Collecting output from an external command is something that I've what to do in clisp for quite a while now; so I'm more than happy to share how I did this with other lispers.
;; a function to collect the contents of a file and return them as a list
(defun collect-file-contents (file)
(with-open-file (stream file)
(loop for
line = (read-line stream nil 'eof)
until (eq line 'eof)
collect line)
))
;; a function that parses output-modes for the exec function.
(defun parse-output-mode (&optional mode)
(case mode
;; standard out
((stdout STDOUT OUT out 1 >) " > ")
;; standard error
((stderr STDERR err ERR 2 2>) " 2> ")
;; both
((both BOTH & &>) " &> ")
;; else, just use stdout
(otherwise " > ") ))
;; the function that puts it all together. :P
(defun exec (cmds &optional output-mode)
(let* (
;; output filename
(w ".lisp.out")
;; output redirection to output file.
(x (concatenate 'string (parse-output-mode output-mode) w)) )
;; everything else
(car (cdr (list
;; concatenate, and run with output redirected to output file.
(shell (concatenate 'string (string-right-trim " " cmds) x))
;; collect output contained in the outputfile.
(collect-file-contents w)
;; delete file now that its contents have been collected.
(delete-file w)) )) ))
[1]> (exec "ls / | sed 's/ /\\n/g'")
("bin" "boot" "dev" "etc" "home" "lib" "lib64" "lost+found" "media" "mnt" "opt" "proc" "root" "run" "sbin" "srv" "sys" "tmp" "usr" "var")
[2]> (exec "killall pidgin")
pidgin: no process found
NIL
[3]> (exec "killall pidgin" 'STDERR)
("pidgin: no process found")
Last edited by lspci (2013-04-23 20:32:31)
Offline