How to use sbcl to use shell command

2019-05-13 20:22发布

I want to programing a function about open the file directly. Like python code:

os.system("ls")

For example, when I use this function (fun_open "/path/to/file"), the system will open the file use the default app. If file is a .txt, open it with textedit.

How to make it?

----UPDATE 9/24/2015-----

My code is:

(defun open_by_system (dir)
  (sb-ext:run-program "/usr/bin/open" (list "-a" "Preview" dir)))

and I use it:

CL-USER> (open_by_system "~/Desktop/ML.pdf")
#<SB-IMPL::PROCESS :EXITED 1>

Nothing else happen

3条回答
相关推荐>>
2楼-- · 2019-05-13 20:52

I'd recommend using UIOP, which provides portable interface to the OS and is universally available as a part of ASDF3:

(uiop:run-program "ls")

See the docstrings in run-program.lisp for details.

If you need more convenience functions, you could take a look at inferior-shell.

查看更多
姐就是有狂的资本
3楼-- · 2019-05-13 20:53

I recommend you to take a look at the available libraries on quickdocs:

link

I recommend you to use inferior-shell available on quicklisp

link

loading:

CL-USER> (ql:quickload 'inferior-shell)
To load "inferior-shell":
  Load 5 ASDF systems:
    alexandria asdf closer-mop named-readtables optima
  Install 4 Quicklisp releases:
    fare-mop fare-quasiquote fare-utils inferior-shell
; Fetching #<URL "http://beta.quicklisp.org/archive/fare-quasiquote/2015-06-08/fare-quasiquote-20150608-git.tgz">
; 15.08KB
==================================================
15,437 bytes in 0.10 seconds (157.03KB/sec)
; Fetching #<URL "http://beta.quicklisp.org/archive/fare-utils/2015-06-08/fare-utils-20150608-git.tgz">
; 31.51KB
==================================================
32,264 bytes in 0.14 seconds (218.80KB/sec)
; Fetching #<URL "http://beta.quicklisp.org/archive/fare-mop/2015-06-08/fare-mop-20150608-git.tgz">
; 2.67KB
==================================================
2,738 bytes in 0.00 seconds (0.00KB/sec)
; Fetching #<URL "http://beta.quicklisp.org/archive/inferior-shell/2015-06-08/inferior-shell-20150608-git.tgz">
; 12.87KB
==================================================
13,182 bytes in 0.00 seconds (12873.05KB/sec)
; Loading "inferior-shell"
[package fare-utils]..............................
[package fare-stateful]...........................
[package fare-quasiquote].........................
[package fare-mop].............
(INFERIOR-SHELL)

a simple sample:

CL-USER> (inferior-shell:run/ss '(echo (1) "2" (+ 3)))
"1 2 3"
NIL
0

a sample with pipes:

CL-USER> (inferior-shell:run/ss `(inferior-shell:pipe (echo (+ hel "lo,") world) (tr "hw" "HW") (sed -e "s/$/!/")))
"Hello, World!"
NIL
0
查看更多
劳资没心,怎么记你
4楼-- · 2019-05-13 21:18

Mac OS X and SBCL:

Open a text file in the default text editor application TextEdit:

Lisp Machine:~ lispm$ touch /tmp/test.text

Lisp Machine:~ lispm$ sbcl
This is SBCL 1.2.14, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.

* (sb-ext:run-program "/usr/bin/open" '("/tmp/test.text"))

#<SB-IMPL::PROCESS :EXITED 0>

Open the file with LispWorks as the text editor:

* (sb-ext:run-program
    "/usr/bin/open"
    '("-a"
      "/Applications/LispWorks 7.0 (64-bit)/LispWorks (64-bit).app"
      "/tmp/test.text"))

You might want to consult the SBCL manual for such questions. For example the chapter on Running External Programs.

查看更多
登录 后发表回答