zsh-users
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Fun thing, manual execution of $( ) command substitution
Date: Sun, 20 May 2018 15:59:49 +0200	[thread overview]
Message-ID: <CAKc7PVCu=trXd9WhkH13pq9DaDt5UtpMDA8YBbNA7b6r5YtwVQ@mail.gmail.com> (raw)

Hello,
I wrote an ini parser and it allows to put $(...) in the content
because of following function. It isn't a rocket science, and doesn't
much parse to resolve $( echo "$( )" ) cases, but I think that
animating plain-text files is something interesting. Maybe someone has
other such tricks up sleeve?

The function will replace any $( ... ) in given buffer, with
stdout-result of command inside the parentheses, and return new buffer
in REPLY. It uses extended glob. Somewhat robust – handles $(echo \)).

function subst_cmds {
    local buffer="$1" nul=$'\0'
    local -a cmds

    cmds=( ${(0)${(S)buffer//(#b)*\$\((?#)([^\\]\))/${match[1]}${match[2]%\)}${nul}}%$nul*}
)

    [[ "${cmds[1]}" = "$buffer" ]] && return 0

    integer size="${#cmds}" i
    local -a outputs

    for (( i = 1; i <= size; ++ i )); do
        outputs[i]="$( ${(z)cmds[i]} )"
    done

    i=0
    REPLY="${(S)buffer//(#b)\$\((?#)([^\\]\))/${outputs[++i]}}"

    return 0
}

-- 
Best regards,
Sebastian Gniazdowski


             reply	other threads:[~2018-05-20 14:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-20 13:59 Sebastian Gniazdowski [this message]
2018-05-21 15:38 ` Sebastian Gniazdowski

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKc7PVCu=trXd9WhkH13pq9DaDt5UtpMDA8YBbNA7b6r5YtwVQ@mail.gmail.com' \
    --to=sgniazdowski@gmail.com \
    --cc=zsh-users@zsh.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).