Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Fun thing, manual execution of $( ) command substitution
- X-seq: zsh-users 23397
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Fun thing, manual execution of $( ) command substitution
- Date: Sun, 20 May 2018 15:59:49 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=/S9NQiPX+9z58FIKAmkxo922HIBqHOYEArOnx5e/q+E=; b=hOIX6u/7PT1WKcbHRqCT40CiFz8NjNtlGWKCZyQcPgq0bz1T80QJ4AkPfzjTH6aibk oU26iXGpjlusSY5OxQQVjGrJdWJrvQbEAhC2A7CDBE5NHL/eM8QGBDP6ZlCOsSlXmSdr bfJjKkQIUu7X1Z+BuwHIhs0C3bqyt81jKHwmnO+bMtVMBV2LajT8O7S+rxeE7LDjnnvy SDez85smO0M70KjsFzvo4mlytap2XIREwcDaXNX5g/cAgQCBD7BrfvOLV0ITmu+uPFpb ORCJ1PlGCHwWM2zwEKwimb2QjY+Byri1y1n1VguXzu5BtiDX9BksZ23y0hWqU9+GPYTL UMTg==
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author