From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Nala Ginrut Newsgroups: gmane.lisp.guile.devel Subject: [Proposal] Why not add a "shell" procedure? Date: Sat, 12 May 2012 20:30:21 +0800 Message-ID: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: dough.gmane.org 1336825834 1652 80.91.229.3 (12 May 2012 12:30:34 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 12 May 2012 12:30:34 +0000 (UTC) To: guile-devel Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat May 12 14:30:30 2012 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1STBSw-0003Xl-7m for guile-devel@m.gmane.org; Sat, 12 May 2012 14:30:30 +0200 Original-Received: from localhost ([::1]:54692 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1STBSv-0007Tf-Ij for guile-devel@m.gmane.org; Sat, 12 May 2012 08:30:29 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:45603) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1STBSr-0007TI-Vn for guile-devel@gnu.org; Sat, 12 May 2012 08:30:27 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1STBSq-0001fY-1P for guile-devel@gnu.org; Sat, 12 May 2012 08:30:25 -0400 Original-Received: from mail-vb0-f41.google.com ([209.85.212.41]:60551) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1STBSp-0001el-Qy for guile-devel@gnu.org; Sat, 12 May 2012 08:30:23 -0400 Original-Received: by vbbey12 with SMTP id ey12so4545680vbb.0 for ; Sat, 12 May 2012 05:30:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=AGyxNsdzM+hTYl6gJZpdCDIpL4cTQeJd52sjKoh7wK0=; b=g4YmHzrnHa+aCrp+ecBxW3RossbIWS125cY0EaQMm+JLzMffwzTeQ/oG0FKK5ahPaF O8mGnks0CLps4pHf1OFg7Jvh/KHZj2sGloFoq/Onz2+AWqYN3jYpS+ayhEfcVR0DG78A Ogo4p6iKvwUfVDV0iq/PrLJHP0rynW9X5y0CMbItvfwsTz8g0MLgyMSGuSf5pdGWk7IU Z7+VsTFTIrQe4dvKjFz8ZT4SL8U2ao4HbALc2cjDTTt/bnucfgqJnKXEdPDbYe6XbzOo 1ZlcvpHvf55f7//+d1aeFXffOI2Lqmnv/GPkXCt+j4CU+mYBIhgwHxY7LUMh97YO4Q4U RPRg== Original-Received: by 10.220.223.132 with SMTP id ik4mr635773vcb.38.1336825821348; Sat, 12 May 2012 05:30:21 -0700 (PDT) Original-Received: by 10.52.29.47 with HTTP; Sat, 12 May 2012 05:30:21 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.212.41 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:14398 Archived-At: 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?