guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shell commands with output to string


From: Zelphir Kaltstahl
Subject: Re: Shell commands with output to string
Date: Wed, 23 Feb 2022 01:28:58 +0000

Hi all!

On 2/22/22 12:20, Neil Jerram wrote:
On Tue, 22 Feb 2022 at 10:23, Alex Sassmannshausen
<alex.sassmannshausen@gmail.com> 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.
Another example, for reading transactions out of a Ledger file:

(use-modules (ice-9 popen))

(define (ledger-transactions filename account payee commodity year)
   (let* ((cmd (string-append "ledger -f " filename))
          (cmd-add! (lambda strings (set! cmd (apply string-append cmd
" " strings)))))
     (if payee
         (cmd-add! "-l 'payee=~/" payee "/'"))
     (if year
         (cmd-add! "--begin " (number->string year) " --end "
(number->string (1+ year))))
     (cmd-add! "reg")
     (if account
         (cmd-add! account))
     (cmd-add! "-F '(\"%(format_date(date, \"%Y-%m-%d\"))\" \"%P\" 
\"%(t)\")\n'")
     (let ((p (open-input-pipe cmd)))
       (let loop ((txs '()))
         (let ((tx (read p)))
           (if (eof-object? tx)
               (reverse! txs)
               (begin
                 (if commodity
                     (set-car! (cddr tx) (string-replace-substring
(caddr tx) commodity "")))
                 (loop (cons tx txs)))))))))

I will look at all the ideas and try to get a good picture of it : )

Thank you all for the advice and examples!

Regards,
Zelphir

--
repositories: https://notabug.org/ZelphirKaltstahl




reply via email to

[Prev in Thread] Current Thread [Next in Thread]