Executing a shell command from Common Lisp

后端 未结 8 1665
耶瑟儿~
耶瑟儿~ 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:29

    This (appupdate.cl) program is an example of creating and executing a shell script using the Steel Bank Common Lisp (sbcl) implementation, which assumes you have sbcl installed and its in your path.

    I wrote this on Ubuntu 14.04 as a simple way to perform the automation of the updating, upgrading, and kernel upgrading of the app/system software.

    #!/usr/local/bin/sbcl --script
    (with-open-file (str "/home/geo/update.sh"
                         :direction :output
                         :if-exists :supersede
                         :if-does-not-exist :create)
      (format str "#! /bin/bash~%~%apt-get update~%~%apt-get upgrade -y~%~%apt-get dist-upgrade -y~%~%exit~%))
    (sb-ext:run-program "/bin/chmod" '("+x" "/home/geo/update.sh")
        :output *standard-output*)
    (sb-ext:run-program "/bin/bash" '("/home/geo/update.sh")
        :output *standard-output*)
    (sb-ext:run-program "/bin/rm" '("-rf" "/home/geo/update.sh")
        :output *standard-output*)
    

    So of course it creates a shell script entitled update.sh, which is directed to /bin/bash via shebang (#!). After doing so the sb-ext:run-program built directs a shell to execute /bin/chmod passing the flag "+x" as an argument and the /path/to/the-file. This function changes the mode of access of the file to executable (changes the permissions).

    Next, a shell is open and executes /bin/bash and the bash binary is passed the argument of the executable shell scripts file location.

    Lastly the file is removed from the working directory (note in this case the appupdate.cl is in my home directory therefore is the working directory).

    The appupdate.cl file can be executed from the command line after it is changed to executable and temporary root privileges are gained:

    :~$ chmod +x appupdate.cl
    
    :~$ sudo bash
    
    :~# ./appupdate.cl
    
    :~# exit
    

    Easily enough the sudo command could be added to the script (e.g. sudo apt-get update) and using the sudo bash sequence would not be necessary.

    NOTE: In the LispWorks ide on 14.04 the (sys:run-shell-command "") is still applicable even though it has sort of become a 'legacy' function.

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

    You can consider using Trivial-shell (url)

    (trivial-shell:shell-command "echo foo")
    

    shell-command returns output, so you can assign it to a variable.

    In asdf.lisp file you can read:

    ;;;; We probably should move this functionality to its own system and deprecate

    ;;;; use of it from the asdf package. However, this would break unspecified

    ;;;; existing software, so until a clear alternative exists, we can't deprecate

    ;;;; it, and even after it's been deprecated, we will support it for a few

    ;;;; years so everyone has time to migrate away from it. -- fare 2009-12-01

    0 讨论(0)
提交回复
热议问题