如何使用sbcl执行shell命令

5

我希望编写一个直接打开文件的函数,就像 Python 代码一样:

os.system("ls")

例如,当我使用这个函数(fun_open“/path/to/file”)时,系统会使用默认应用程序打开文件。如果文件是.txt,则使用textedit打开。
如何实现?
---- 更新9/24/2015 -----
我的代码是:
(defun open_by_system (dir)
  (sb-ext:run-program "/usr/bin/open" (list "-a" "Preview" dir)))

我使用它:

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

没有其他事件发生


“默认应用程序”是什么意思?这是哪个操作系统? - melpomene
1
可能是重复的问题:SBCL运行Shell命令 - Joshua Taylor
3
我认为你在混淆两个问题。一个是如何运行shell命令(根据其他问题,使用sb-ext:run-program)。但是你描述的另一个行为(用“默认”应用程序打开文件)通常由系统上的另一个实用程序提供。例如,在某些Linux机器上,您可以运行xdg-open file以在“默认”查看器/应用程序中打开file。在Windows上,我认为你可以使用explorer file,而在OS X上则是open file。(但我不知道这些是否是最佳选项,只是过去对我有效的选项。) - Joshua Taylor
1
@JoshuaTaylor 在Windows上通常的命令是start file。我很惊讶资源管理器能够工作。 - Random832
@Random832,我从未花太多时间研究过它,但是是的,资源管理器适用于许多事情([看起来我不是唯一遇到这种问题的人](http://superuser.com/a/844405/240381))。 不过,似乎start才是更好的选择。 - Joshua Taylor
显示剩余2条评论
3个回答

4
我建议使用UIOP,它提供了一个便携式的接口到操作系统,并且作为ASDF3的普遍部分普遍可用。
(uiop:run-program "ls")

有关详细信息,请参阅run-program.lisp中的docstrings。

如果您需要更多的便利函数,可以查看inferior-shell


3
我建议您查看quickdocs上提供的库: 链接 我建议使用quicklisp上可用的inferior-shell: 链接 加载:
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)

一个简单的示例:
CL-USER> (inferior-shell:run/ss '(echo (1) "2" (+ 3)))
"1 2 3"
NIL
0

一个使用管道的示例:

CL-USER> (inferior-shell:run/ss `(inferior-shell:pipe (echo (+ hel "lo,") world) (tr "hw" "HW") (sed -e "s/$/!/")))
"Hello, World!"
NIL
0

1
Mac OS X和SBCL
在默认的文本编辑器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>

使用LispWorks作为文本编辑器打开文件:

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

您可能希望查阅SBCL手册以获取相关问题的答案。例如,运行外部程序章节。

“sb-ext:run-program”是在可能的重复问题中提出的答案,即SBCL运行Shell命令 - Joshua Taylor
我编写了这个函数,但是退出代码是1,然后TextEdit(实际上我打开的是PDF,可能是预览应用程序)没有打开。 - ccQpein
@JoshuaTaylor 对此我感到很抱歉!我的目标是使OS X打开系统中安装的应用程序。我使用了您的解决方案,并成功编译,但除非给我“<SB-IMPL :: PROCESS:EXITED 1>”,否则什么也不会发生。例如,我想通过CL使用Preview.app打开test.pdf。对于我的英语表示歉意。 - ccQpein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接