running shell commands with gnu clisp

前端 未结 4 2051
灰色年华
灰色年华 2021-01-06 08:29

I\'m trying to create a \"system\" command for clisp that works like this

(setq result (system \"pwd\"))

;;now result is equal to /my/path/here
相关标签:
4条回答
  • 2021-01-06 08:57

    You are asking specifically about clisp. I'll add here that if you are using Clozure CL then you can also easily run os subprocesses.

    Some examples:

    ;;; Capture the output of the "uname" program in a lisp string-stream
    ;;; and return the generated string (which will contain a trailing
    ;;; newline.)
    ? (with-output-to-string (stream)
        (run-program "uname" '("-r") :output stream))
    ;;; Write a string to *STANDARD-OUTPUT*, the hard way.
    ? (run-program "cat" () :input (make-string-input-stream "hello") :output t)
    ;;; Find out that "ls" doesn't expand wildcards.
    ? (run-program "ls" '("*.lisp") :output t)
    ;;; Let the shell expand wildcards.
    ? (run-program "sh" '("-c" "ls *.lisp") :output t)
    

    Do a search for run-program in the CCL docs located here: http://ccl.clozure.com/ccl-documentation.html

    There are a couple nice Lisp ways of doing this in this stackoverflow answer: Making a system call that returns the stdout output as a string Once again, Rainer to the rescue. Thanks Ranier.

    0 讨论(0)
  • 2021-01-06 09:07

    This is a shorter one

    (defun system(cmd)
      (ext:shell (string cmd)))
    
    > (system '"cd ..; ls -lrt; pwd")
    
    0 讨论(0)
  • 2021-01-06 09:09

    Per the CLISP documentation on run-program, the :output argument should be one of

    • :terminal - writes to the terminal
    • :stream - creates and returns an input stream from which you can read
    • a pathname designator - writes to the designated file
    • nil - ignores the output

    If you're looking to collect the output into a string, you'll have to use a read-write copying loop to transfer the data from the returned stream to a string. You already have with-output-to-string in play, per Rainer's suggestion, but instead of providing that output stream to run-program, you'll need to write to it yourself, copying the data from the input stream returned by run-program.

    0 讨论(0)
  • 2021-01-06 09:15

    Something like this?

    Version 2:

    (defun copy-stream (in out)
       (loop for line = (read-line in nil nil)
             while line
             do (write-line line out)))
    
    (defun system (cmd)
      (with-open-stream (s1 (ext:run-program cmd :output :stream))
        (with-output-to-string (out)
          (copy-stream s1 out))))
    
    
    [6]> (system "ls")
    "#.emacs#
    Applications
    ..."
    
    0 讨论(0)
提交回复
热议问题