2

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?

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

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

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.