unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Nala Ginrut <nalaginrut@gmail.com>
To: guile-devel <guile-devel@gnu.org>
Subject: [Proposal] Why not add a "shell" procedure?
Date: Sat, 12 May 2012 20:30:21 +0800	[thread overview]
Message-ID: <CAPjoZof=EYj_eUyZSd1Pzcnx=2eTt22rQ_+gYS4UDb8hitijig@mail.gmail.com> (raw)

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	other threads:[~2012-05-12 12:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-12 12:30 Nala Ginrut [this message]
2012-05-12 12:35 ` [Proposal] Why not add a "shell" procedure? Nala Ginrut
2012-05-12 14:22 ` Thien-Thi Nguyen
2012-05-13 15:04   ` Nala Ginrut
2012-05-13 18:29 ` Mark H Weaver
2012-05-14  0:17   ` Nala Ginrut
  -- strict thread matches above, loose matches on Subject: below --
2012-05-12 14:37 Nelson H. F. Beebe
2012-05-13 15:08 ` Nala Ginrut

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAPjoZof=EYj_eUyZSd1Pzcnx=2eTt22rQ_+gYS4UDb8hitijig@mail.gmail.com' \
    --to=nalaginrut@gmail.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).