* Fun thing, manual execution of $( ) command substitution
@ 2018-05-20 13:59 Sebastian Gniazdowski
2018-05-21 15:38 ` Sebastian Gniazdowski
0 siblings, 1 reply; 2+ messages in thread
From: Sebastian Gniazdowski @ 2018-05-20 13:59 UTC (permalink / raw)
To: Zsh Users
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Fun thing, manual execution of $( ) command substitution
2018-05-20 13:59 Fun thing, manual execution of $( ) command substitution Sebastian Gniazdowski
@ 2018-05-21 15:38 ` Sebastian Gniazdowski
0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Gniazdowski @ 2018-05-21 15:38 UTC (permalink / raw)
To: Zsh Users
On 20 May 2018 at 15:59, Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> [[ "${cmds[1]}" = "$buffer" ]] && return 0
I have a bug-fix, someone might one day use it and become confused –
REPLY isn't set to "$buffer" in the line above. So:
[[ "${cmds[1]}" = "$buffer" ]] && { REPLY="$buffer"; return 0; }
I.e. no $( ) areas detected in buffer -> return unchanged buffer (no
command substitution done).
--
Best regards,
Sebastian Gniazdowski
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2018-05-21 15:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-20 13:59 Fun thing, manual execution of $( ) command substitution Sebastian Gniazdowski
2018-05-21 15:38 ` Sebastian Gniazdowski
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).