guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Proposal] Why not add a "shell" procedure?


From: Nala Ginrut
Subject: Re: [Proposal] Why not add a "shell" procedure?
Date: Sat, 12 May 2012 20:35:26 +0800

Well, sed is an improper example, I should have deleted "-i":
 (shell "sed \"s:guile/Guile/g" somefile")

On Sat, May 12, 2012 at 8:30 PM, Nala Ginrut <address@hidden> wrote:
> hi folks!
> Sometimes we need to run shell script and get the result as string type.
> Say, in Ruby:
> irb: `ls`
> ==> 
> "ice-9\nlanguage\nMakefile.am\nMakefile.am~\nMakefile.in\noop\nrnrs\nrnrs.scm\nscripts\nsrfi\nstatprof.scm\nsxml\nsystem\ntexinfo\ntexinfo.scm\nweb\n"
>
> * Note: "system" lib function is useless for this, because "system"
> can't return the result as string, but just the retval.
>
> This feature is very easy to implement in Guile, but I recommend to
> add a global env-var %current-shell to point any shell-interpreter,
> like csh/bash/sh/..., or modify it as user wish.
> The "shell" implementation maybe like this:
> -------------code----------------
> (define %current-shell (getenv "SHELL"))
> (use-modules (ice-9 popen) (rnrs io ports))
> (define shell
>   (lambda (cmd)
>       (let ((str (string-append %current-shell " -c " cmd)))
>          (get-string-all (open-pipe str OPEN_READ)))))
> -------------end----------------
>
> and use it like regular shell:
> (shell "sed -i \"s:guile/Guile/g" somefile")
>
> Moreover, we can implement "pwd" with this "shell" easily:
> ----------code----------
> (use-module (ice-9 rdelim))
> (define (pwd)
>  (call-with-input-string (shell "pwd")
>                                     (lambda (port) (read-line port))))
> -----------end-----------
>
> (pwd)
> ==> "/home/nalaginrut/Project/gnulib-20100109+stable"
>
> Any comment?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]