How to use sbcl to use shell command
Asked Answered
N

3

5

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

Nates answered 21/9, 2015 at 19:38 Comment(7)
What do you mean by "default app"? What OS is this?Dilorenzo
possible duplicate of SBCL Run Shell CommandEmersed
@Nates I think you're confusing two issues here. One is how to run a shell command (according to other question, use sb-ext:run-program). But then the other behavior you're describing (opening the file with a "default" application) is usually provided by another utility on the system. E.g., on some Linux machines, you could run xdg-open file to open file in the "default" viewer/application. On Windows, I think you can use explorer file, and on OS X open file. (But I don't know whether any of those are the best options, just ones that have worked for me in the past.)Emersed
@JoshuaTaylor The usual command on windows is start file. I'm surprised explorer works.Quarterphase
@Quarterphase I'd never spent much time looking into it, but yeah, explorer works for a lot of things (and it looks like I'm not the only one who's come across it). It does appear that start is the better option, though.Emersed
@JoshuaTaylor you are right, I am not confusing the difference, but my english skill can't express that. Thank you anyway! And now I am searching how to solve the exited code question.Nates
"Preview" is not a useful path. See my example. You might also look at the documentation for open -a, to see what it expects.Tucket
I
3

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
Irreplaceable answered 24/9, 2015 at 21:15 Comment(0)
I
4

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.

Implore answered 24/9, 2015 at 20:24 Comment(0)
I
3

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
Irreplaceable answered 24/9, 2015 at 21:15 Comment(0)
T
1

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.

Tucket answered 21/9, 2015 at 20:42 Comment(3)
sb-ext:run-program is the answer that came up in the possible duplicate, SBCL Run Shell Command, too.Emersed
I code the function, but the exit code is 1, and textedit(actually I open pdf it maybe preview.app) don't open.Nates
@JoshuaTaylor I'm sorry about that! My target is make OS X open file with App installed in system. I use your solution, and compile good, but nothing happen unless give me "<SB-IMPL::PROCESS :EXITED 1>". For instance, I want open test.pdf by Preview.app with CL. I'm sorry for my english.Nates

© 2022 - 2024 — McMap. All rights reserved.