Executing a shell command from Common Lisp

后端 未结 8 1664
耶瑟儿~
耶瑟儿~ 2020-12-14 01:53

How can i execute a shell (bash) command within a Common Lisp program and assign the output to a variable?

相关标签:
8条回答
  • 2020-12-14 02:09

    Nowadays I would use uiop:run-program, where uiop stands for "universal input output" and is a compatibility layer provided by asdf3, formerly known as asdf/driver. As has been said asdf:run-shell-command is obsolete and uiop inherits many features of other libraries such as trivial-shell.

    UIOP readme

    0 讨论(0)
  • 2020-12-14 02:12

    ASDF provides a RUN-SHELL-COMMAND that works with many Common Lisp implementations including ABCL, Allegro CL, CLISP, Clozure CL, ECL, GCL, LispWorks, SBCL, CMU, XCL and SCL.

    It takes a control string and a list of arguments like FORMAT, and synchronously executes the result using a Bourne-compatible shell. Capture output by binding an optional stream.

    0 讨论(0)
  • 2020-12-14 02:12

    In sbcl:

    (sb-ext:run-program "/bin/sh" (list "-c" "whoami") :input nil :output *standard-output*)
    

    It works fine for me:)

    0 讨论(0)
  • 2020-12-14 02:14

    I tried out some answers but it was not straightforward. This is what worked easily:

    (ql:quickload "external-program")
    ;; run shell command e.g. "ls -l" and capture the output into string *output*
    (defparameter *output* 
                  (with-output-to-string (out) 
                    (external-program:run "ls" '("-l")  ; command with parameters as list of strings
                                          :output out)))
    ;; and after that, you can write functions to parse the output ...
    

    This is from Edi Weitz's book Common Lisp Recipes which belongs to the shelve of any serious Lisp programmer, in my view...

    0 讨论(0)
  • 2020-12-14 02:19

    Some CL implementations have built-in functions for this purpose. For example, SBCL has sb-ext:run-program, and CCL has run-program.

    0 讨论(0)
  • 2020-12-14 02:27

    ITA has released inferior-shell under their QITAB umbrella project.

    Some links of possible interest :

    • http://common-lisp.net/gitweb?p=projects/qitab/inferior-shell.git
    • http://common-lisp.net/projects/qitab/
    • http://cliki.net/inferior-shell

    A git repository is currently hosted at common-lisp.net :

    git clone git://common-lisp.net/projects/qitab/inferior-shell.git
    
    0 讨论(0)
提交回复
热议问题