petergil
petergil

Reputation: 180

Using stdout from shell script in common lisp

I am writing a common lisp program that needs to handle the output of a command. However, when I try to use the results in another function, I only get a NIL as return-value.

Here is the function I use to run commands:

(defun run-command (command &optional arguments)
       (with-open-stream (pipe 
                 (ext:run-program command :arguments arguments
                                  :output :stream :wait nil)) 
       (loop
                :for line = (read-line pipe nil nil)
                :while line :collect line)))

Which, when run by itself gives:

CL-USER> (run-command "ls" '("-l" "/tmp/test"))
         ("-rw-r--r-- 1 petergil petergil 0 2011-06-23 22:02 /tmp/test")

However, when I run it through a function, only NIL is returned:

(defun sh-ls (filename)
       (run-command "ls" '( "-l" filename)))
CL-USER> (sh-ls  "/tmp/test")
         NIL

How can I use the results in my functions?

Upvotes: 2

Views: 570

Answers (2)

Spec
Spec

Reputation: 847

You can also use backquote ` before sexpr and , before filename to evaluate it:

(defun sh-ls (filename)
       (run-command "ls" `("-l" ,filename)))

Upvotes: 4

Will Hartung
Will Hartung

Reputation: 118794

Try this:

(defun sh-ls (filename)
       (run-command "ls" (list "-l" filename)))

The '("-l" filename) is quoting the list, AND the symbol 'filename', rather than evaluating filename.

Upvotes: 7

Related Questions