Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: convolutions

07.11.2015, 05:17, "Ray Andrews" <rayandrews@xxxxxxxxxxx>:
> Gentlemen:
>      echo "$(eval echo "\${$(cat in_file)}")" >! out_file
> That's the best I've been able to do expanding color variables, eg. "
> ${red} " into their native " \e[31;1m " in a file. It's not a prize
> winner as far as zsh obfuscation goes, still, can it be done more
> simply? Also, I'd not be surprised to learn that there's one of those
> ancient little utilities that already does that:
>      cat in_file | ancient_utility > outfile
> is there?

“Ancient utility” is called `sed`. Specifically you are able to substitute text with the result of the evaluation of the shell code:

    echo '${red}red' | sed -r 's/\$\{(\w+)\}/zsh -c ''echo ${(%%):-%F{\1}}''/e'

Note: sed uses /bin/sh for /e and not $SHELL, so you must call zsh explicitly.
Note 2: see “SIMPLE PROMPT ESCAPES” (specifically `%F`) in `man zshmisc`. See “PARAMETER EXPANSION”/“Parameter Expansion Flags” in `man zshexpn` (specifically `%` flag). See `man zshoptions`, option `RCQUOTES` if you are wondering why I wrote `''`.

You may also do the same thing with almost every other script language. But while in perl this is mostly as verbose as in sed, most other are less oriented on text processing and will be more verbose. Also sed and awk are rather standard, perl and python also are to a less extent, everything else better be avoided in shell scripts.

Messages sorted by: Reverse Date, Date, Thread, Author