From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: Olivier Dion <olivier.dion@polymtl.ca>,
Leo Butler <leo.butler@umanitoba.ca>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Shell commands with output to string
Date: Wed, 23 Feb 2022 01:26:44 +0000 [thread overview]
Message-ID: <9a84554c-c2ef-7c9b-ffff-129cb517afca@posteo.de> (raw)
In-Reply-To: <87k0dm6etu.fsf@laura>
Hi Oliver!
On 2/22/22 17:33, Olivier Dion wrote:
> On Tue, 22 Feb 2022, Leo Butler <leo.butler@umanitoba.ca> wrote:
>> Olivier Dion via General Guile related discussions <guile-user@gnu.org>
>> writes:
>>
>>> On Tue, 22 Feb 2022, Zelphir Kaltstahl <zelphirkaltstahl@posteo.de> 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
next prev parent reply other threads:[~2022-02-23 1:26 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-22 9:29 Shell commands with output to string Zelphir Kaltstahl
2022-02-22 9:38 ` Zelphir Kaltstahl
2022-02-22 10:20 ` Alex Sassmannshausen
2022-02-22 10:43 ` post
2022-02-23 14:01 ` Josselin Poiret
2022-03-08 23:12 ` Zelphir Kaltstahl
2022-03-09 14:14 ` Josselin Poiret
2022-02-22 11:20 ` Neil Jerram
2022-02-23 1:28 ` Zelphir Kaltstahl
2022-02-23 1:29 ` Zelphir Kaltstahl
2022-02-22 10:21 ` tomas
2022-02-22 14:27 ` Olivier Dion via General Guile related discussions
2022-02-22 16:00 ` Leo Butler
2022-02-22 16:33 ` Olivier Dion via General Guile related discussions
2022-02-23 1:26 ` Zelphir Kaltstahl [this message]
2022-02-23 14:13 ` Olivier Dion via General Guile related discussions
2022-02-26 0:32 ` Zelphir Kaltstahl
-- strict thread matches above, loose matches on Subject: below --
2022-02-23 17:48 Blake Shaw
2022-02-23 18:25 ` Olivier Dion via General Guile related discussions
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=9a84554c-c2ef-7c9b-ffff-129cb517afca@posteo.de \
--to=zelphirkaltstahl@posteo.de \
--cc=guile-user@gnu.org \
--cc=leo.butler@umanitoba.ca \
--cc=olivier.dion@polymtl.ca \
/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).