unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Re: [Proposal] Why not add a "shell" procedure?
@ 2012-05-12 14:37 Nelson H. F. Beebe
  2012-05-13 15:08 ` Nala Ginrut
  0 siblings, 1 reply; 8+ messages in thread
From: Nelson H. F. Beebe @ 2012-05-12 14:37 UTC (permalink / raw)
  To: guile-devel; +Cc: beebe

Nala Ginrut <nalaginrut@gmail.com> writes today about adding a
function to return a string from a command executed by a shell,
because (system "...") only returns the exit status of the last
command.

The one point that I think is important is the handling of whitespace.

The Unix shells in their backquoted `cmd` and $(cmd) syntax normalize
the output of cmd to replace runs of whitespace [ \n\r\v\t]+ by single
spaces.  

For shell use, that is convenient.  However, inside guile, it may be
more useful to preserve the whitespace as is, and particularly the
line breaks.  

A user who wants whitespace collapsed to single spaces can easily do
so with a regexp substitution, and that could be facilitated by
providing a standard guile library function to do the job
consistently.

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                    FAX: +1 801 581 4148                  -
- Department of Mathematics, 110 LCB    Internet e-mail: beebe@math.utah.edu  -
- 155 S 1400 E RM 233                       beebe@acm.org  beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe/ -
-------------------------------------------------------------------------------



^ permalink raw reply	[flat|nested] 8+ messages in thread
* [Proposal] Why not add a "shell" procedure?
@ 2012-05-12 12:30 Nala Ginrut
  2012-05-12 12:35 ` Nala Ginrut
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Nala Ginrut @ 2012-05-12 12:30 UTC (permalink / raw)
  To: guile-devel

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?



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-05-14  0:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-12 14:37 [Proposal] Why not add a "shell" procedure? Nelson H. F. Beebe
2012-05-13 15:08 ` Nala Ginrut
  -- strict thread matches above, loose matches on Subject: below --
2012-05-12 12:30 Nala Ginrut
2012-05-12 12:35 ` 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

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).