unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: post@thomasdanckaert.be
To: alex.sassmannshausen@gmail.com
Cc: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>, guile-user@gnu.org
Subject: Re: Shell commands with output to string
Date: Tue, 22 Feb 2022 11:43:13 +0100	[thread overview]
Message-ID: <6d1232e036287a4cc7994d9fac5fceec@thomasdanckaert.be> (raw)
In-Reply-To: <87tucryzdg.fsf@gmail.com>

Hi,

to throw in an example: I once used a function like the one below to 
handle stdout and stderr from external commands (from 
https://github.com/tdanckaert/jobview/blob/master/jobtools.scm#L38 ).  
Probably far from perfect (my first and only scheme project...), but 
hopefully it gives you an idea.

(define (process-output proc cmd)
   "Runs CMD as an external process, with an input port from which the
process' stdout may be read, and runs the procedure PROC that takes
this input port as a single argument.  Throws an exception 'cmd-failed
if CMD's exit status is non-zero."
   (let* ((err-pipe (pipe))
	 (err-write (cdr err-pipe))
	 (err-read (car err-pipe))
	 (stderr (current-error-port)))
     (with-error-to-port err-write
       (lambda ()
	(let* ((port (open-input-pipe cmd))
	       (ignore (setvbuf port 'block))
	       (result
		(catch #t
		  ;; Catch any exception thrown by applying PROC to
		  ;; the output of CMD: if CMD fails, we check the
		  ;; exit status below; if CMD succeeds, PROC must be
		  ;; able to deal with its output.
		  (lambda () (proc port))
		  (lambda (key . args)
		    (format stderr "Caught exception ~a from ~y~%" key proc))))
	       (status (close-pipe port)))
	  (close-port err-write)
	  (or (zero? status)
	      (throw 'cmd-failed cmd status
		     (get-string-all err-read)))
	  result)))))

Thomas


On 2022-02-22 11:20, Alex Sassmannshausen wrote:
> Hi Zelphir,
> 
> I think you want to be using the popen / pipe procedures for this. See
> https://www.gnu.org/software/guile/docs/docs-2.2/guile-ref/Pipes.html
> for the chapter in the manual.
> 
> Hope this helps :)
> 
> Alex
> 
> Zelphir Kaltstahl <zelphirkaltstahl@posteo.de> writes:
> 
>> Corrections below.
>> 
>> On 2/22/22 10:29, 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)
>>> 
>>> So far I have found
>>> 
>>> ~~~~
>>> (system ...)
>>> ~~~~
>>> 
>>> which I tried to use with
>>> 
>>> ~~~~
>>> scheme@(guile-user)> (with-output-to-string
>>>   (system "ls -al"))
>>> 
>>> ;; lots of output immediately shown and not stored in variable
>>> 
>>> ice-9/boot-9.scm:1685:16: In procedure raise-exception:
>>> Wrong type to apply: 0
>>> 
>>> Entering a new prompt.  Type `,bt' for a backtrace or `,q' to 
>>> continue.
>>> scheme@(guile-user) [1]> ,bt
>>> In ice-9/ports.scm:
>>>     476:4  2 (with-output-to-string 0)
>>> While executing meta-command:
>>> In procedure frame-local-ref: Argument 2 out of range: 1
>>> ~~~~
>>> 
>>> But this does not give me a string back.
>>> 
>>> I also tried with
>>> 
>>> ~~~~
>>> scheme@(guile-user)> (call-with-values (lambda () (system "ls -al"))
>>> ... (lambda (exit-code output) output))
>>> 
>>> ;; lots of output immediately shown
>>> 
>>> ice-9/boot-9.scm:1685:16: In procedure raise-exception:
>>> Wrong number of values returned to continuation (expected 2)
>>> 
>>> Entering a new prompt.  Type `,bt' for a backtrace or `,q' to 
>>> continue.
>>> 
>>> scheme@(guile-user) [1]> ,bt
>>> In current input:
>>>     10:29  1 (_)
>>> In ice-9/boot-9.scm:
>>>   1685:16  0 (raise-exception _ #:continuable? _)
>>> ~~~~
>>> 
>>> Is there another function I should be using?
>>> 
>>> I would like to have the exit code and the output of a command.
>>> 
>>> Best regards,
>>> Zelphir
>> 
>> Of course I should use `with-output-to-string` correctly:
>> 
>> ~~~~
>> scheme@(guile-user)> (with-output-to-string
>>   (lambda () (system "ls -al")))
>> ;; directly outputted stuff
>> $1 = ""
>> ~~~~
>> 
>> But still not a win.
>> 
>> Regards,
>> Zelphir



  reply	other threads:[~2022-02-22 10:43 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 [this message]
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
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=6d1232e036287a4cc7994d9fac5fceec@thomasdanckaert.be \
    --to=post@thomasdanckaert.be \
    --cc=alex.sassmannshausen@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=zelphirkaltstahl@posteo.de \
    /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).