[Top][All Lists]

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

Re: Shell commands with output to string

From: Neil Jerram
Subject: Re: Shell commands with output to string
Date: Tue, 22 Feb 2022 11:20:03 +0000

On Tue, 22 Feb 2022 at 10:23, Alex Sassmannshausen
<> wrote:
> Hi Zelphir,
> I think you want to be using the popen / pipe procedures for this. See
> 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)
                (if commodity
                    (set-car! (cddr tx) (string-replace-substring
(caddr tx) commodity "")))
                (loop (cons tx txs)))))))))

reply via email to

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