From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Olivier Dion via General Guile related discussions Newsgroups: gmane.lisp.guile.user Subject: Re: Shell commands with output to string Date: Tue, 22 Feb 2022 11:33:49 -0500 Message-ID: <87k0dm6etu.fsf@laura> References: <87o82z5636.fsf@laura> <87y222lwn3.fsf@t14.reltub.ca> Reply-To: Olivier Dion Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14386"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Zelphir Kaltstahl To: Leo Butler , Olivier Dion via General Guile related discussions Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Tue Feb 22 17:52:20 2022 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nMYOg-0003RI-Uu for guile-user@m.gmane-mx.org; Tue, 22 Feb 2022 17:52:18 +0100 Original-Received: from localhost ([::1]:51136 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nMYOf-000774-Gi for guile-user@m.gmane-mx.org; Tue, 22 Feb 2022 11:52:17 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:45784) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMY76-0007GS-OL for guile-user@gnu.org; Tue, 22 Feb 2022 11:34:08 -0500 Original-Received: from smtp.polymtl.ca ([132.207.4.11]:35088) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMY6v-0008Ok-C7 for guile-user@gnu.org; Tue, 22 Feb 2022 11:34:08 -0500 Original-Received: from localhost (modemcable094.169-200-24.mc.videotron.ca [24.200.169.94]) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 21MGXnLq003861 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 22 Feb 2022 11:33:54 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 21MGXnLq003861 In-Reply-To: <87y222lwn3.fsf@t14.reltub.ca> X-Poly-FromMTA: (modemcable094.169-200-24.mc.videotron.ca [24.200.169.94]) at Tue, 22 Feb 2022 16:33:49 +0000 Received-SPF: pass client-ip=132.207.4.11; envelope-from=olivier.dion@polymtl.ca; helo=smtp.polymtl.ca X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:18123 Archived-At: On Tue, 22 Feb 2022, Leo Butler wrote: > Olivier Dion via General Guile related discussions > writes: > >> On Tue, 22 Feb 2022, Zelphir Kaltstahl wrote: >>> Hello Guile users! >>> >>> How would I run a shell command from inside Guile and get its output >>> as a string, instead of the output being outputted directly? (Guile >>> 3.0.8) >> >> I use the following: >> >> (define-module (shell utils) >> #:use-module (ice-9 format) >> #:use-module (ice-9 popen) >> #:use-module (ice-9 textual-ports)) >> >> (define (shell% proc fmt . args) >> (let* ((port (open-input-pipe (format #f "~?" fmt args))) >> (output (proc port))) >> (close-pipe port) >> output)) > > You probably want to inspect the exit value of the shell process, so > that you can handle/throw the error. This is what I use (similar to your > `shell'): You're absolutely right. It would also probably a good idea to use dynamic-wind for close-pipe in case an exception is thrown in read-string I think. > > (define* (shell-command-to-string cmd) > (catch 'shell-command-error > ;; thunk > (lambda () > (let* ((port (open-pipe cmd OPEN_READ)) > (str (read-string port)) > (wtpd (close-pipe port)) > (xval (status:exit-val wtpd))) > (if (or (eqv? xval #f) (> xval 0)) (throw 'shell-command-error cmd str)) > str)) > ;; handler > (lambda (key cmd str) > (simple-format #t "ERROR: in command ~a\nstring: ~a\n" cmd str) > (throw 'error-in-shell-command-to-string cmd str)))) > >> >> (define-public (shell . args) >> (apply shell% (cons get-string-all args))) >> >> (define-public (shell$ . args) >> (apply shell% (cons get-line args))) >> >> Then >> (shell "ls" "-l") >> >> The $ variant is to get a single line in the output. > > I wonder why there is no module already in ice-9 which does this > stuff? It seems like a lot of people are re-inventing the wheel. There's ton of missing stuffs in the standard library IMO. On top of my head, filesystem paths manipulation (e.g. path-join) is also one that is probably getting re-invented a lots. I believe that the successful story of Python is not just about its pretty syntax, but also dues to its very large standard library. -- Olivier Dion Polymtl