Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a shell command from Common Lisp

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

like image 645
Erhan Bagdemir Avatar asked May 19 '11 22:05

Erhan Bagdemir


People also ask

What is $() in shell script?

Seems like ${variable} is the same as $variable , while $() is to execute a command.

What does $() mean in bash?

Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).


4 Answers

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 
like image 111
thodg Avatar answered Sep 28 '22 12:09

thodg


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.

like image 28
Terje Norderhaug Avatar answered Sep 28 '22 12:09

Terje Norderhaug


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

like image 41
Spec Avatar answered Sep 28 '22 12:09

Spec


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

like image 34
Michael E. Avatar answered Sep 28 '22 10:09

Michael E.