From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Re: Shell commands with output to string Date: Wed, 23 Feb 2022 01:26:44 +0000 Message-ID: <9a84554c-c2ef-7c9b-ffff-129cb517afca@posteo.de> References: <87o82z5636.fsf@laura> <87y222lwn3.fsf@t14.reltub.ca> <87k0dm6etu.fsf@laura> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16822"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Guile User To: Olivier Dion , Leo Butler Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Wed Feb 23 02:27:05 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 1nMgQq-0004DV-Ll for guile-user@m.gmane-mx.org; Wed, 23 Feb 2022 02:27:04 +0100 Original-Received: from localhost ([::1]:60714 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nMgQp-0002kP-5m for guile-user@m.gmane-mx.org; Tue, 22 Feb 2022 20:27:03 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:37728) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMgQe-0002kG-4O for guile-user@gnu.org; Tue, 22 Feb 2022 20:26:52 -0500 Original-Received: from mout02.posteo.de ([185.67.36.66]:53379) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMgQb-0004WE-4C for guile-user@gnu.org; Tue, 22 Feb 2022 20:26:51 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 765A9240103 for ; Wed, 23 Feb 2022 02:26:46 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1645579606; bh=4nP9Dtw2HHyY7tvFKD1eHzdriqv6xOBc3/HZf6XTZvs=; h=Date:Subject:To:Cc:From:From; b=fbnzYgFuMU8ppTpdZGWZwc6B9q0tMuJoUd95fBwFqs5UB+c0nMovC5UT80hkgmGLv QmIxoWod9FvE1EYXPf2oXTNEb3CDeTI2tZfsu6aVO6rjh6uSZXWF2PqhCI5H9v/Hm3 OaSJJGWQmnufPGz3jmMiBeRB+63LsTEzBHOxErQE+K1QqYcjrfm1htgArBENXrh39F r3MPGKNRIrCTS/+g7xFTEJnTf5wcgXH1ST9S0M6v/5/UZ/E+YSy0qQQ1h7r/7G5LEk RSzpkBqrkRyCAGTJ4ubvj0C4gCR8rfPtGgmZk3mcp/QmC1bjQnwbPf8UMjJDUVSLX+ hsRztNnyP9mag== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4K3JKd3czjz9rxL; Wed, 23 Feb 2022 02:26:45 +0100 (CET) Content-Language: en-US In-Reply-To: <87k0dm6etu.fsf@laura> Received-SPF: pass client-ip=185.67.36.66; envelope-from=zelphirkaltstahl@posteo.de; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=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:18125 Archived-At: Hi Oliver! On 2/22/22 17:33, Olivier Dion wrote: > 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 actually made something for that, trying to copy mostly the Python behavior for os.path.join: https://notabug.org/ZelphirKaltstahl/guile-fslib Also available as a GNU Guix package, but not updated in a while on Guix. Repository contains more up to date version. > I believe that the successful story of Python is not just about its > pretty syntax, but also dues to its very large standard library. I think so too. Although I sometimes have the feeling, that Guile does things in a cleaner way, once one figures out how to do them in the first place. One thing I really like are the ports. Stuff like call-with-output-string. Takes some twisting of the brain, but once one gets it, it becomes very useful and elegant. But yes, Python is very beginner friendly in terms of batteries included. Although I think that its syntax feels a bit ad-hoc. As in "Oh we want some syntax for X … lets invent this keyword here." or some new operators or things like that. I like Guile syntax (or Scheme in general) much more. However, it is difficult to motivate others in a quick demo to learn the language, when you cannot take half an hour time to explain, what that for other people weird looking syntax is actually really cool. Regards, Zelphir -- repositories: https://notabug.org/ZelphirKaltstahl