From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: Re: unmatched '
Date: Fri, 9 Mar 2018 14:45:24 -0800 [thread overview]
Message-ID: <18dac66c-f348-8123-c051-4deb3dd21294@eastlink.ca> (raw)
In-Reply-To: <CAH+w=7ZSyniDaVOgKhzPrdchv2gtVM0G0+JgwwJXYOYctvgg+g@mail.gmail.com>
On 09/03/18 12:23 PM, Bart Schaefer wrote:
> You really should never do this with "eval". Consider what happens if the
> file name is /etc/Why you should never `rm -rf /`
>
> (Obligatory warning: DO NOT TRY THAT AT HOME.)
I probably have a lot of improper constructions here, I use the first
thing that seems to work, and later I repent of various sins as they
become apparent. How would I best do that sort of thing?
> Why are you putting stuff like that file in /etc/ in the first place?
Heck, not me! That's stock Debian, I'd not create a directory like that
on pain of damnation. Debian should not allow it either, IMHO.
> If $bar is a file name with spaces in it,
$bar will start with a list of directories as originally output by
'find', so whatever my code, it has to handle whatever 'find' might find.
> Based on your description there must be a leading "|" in $grepstring.
> Don't do that.
Yeah:
ccolor=33 # Start with yellow, then blue, magenta, cyan.
for file in "$@"; do
# We colorize the already selected lines here:
grepstring="$grepstring | GREP_COLOR='01;'"$ccolor" grep $wwild
$ccase --color=always \"$file\""
(( ccolor++ )) # Next color.
done
... so I'm filtering and colorizing however many arguments there are to
the command, so each iteration must (?) begin with the pipe. And the
final string:
bar=(`eval "find -H -O3 / -warn -type d -ipath \"*$1*\""
$grepstring`)
> Assuming you've carefully sanitized $grepstring to avoid gotchas like
> unintentional `...` or $(...) substitutions, the closest you should
> ever get to what you're doing is
>
> baz=( `print -lr -- "${bar[@]}" | eval "$grepstring"` )
>
> There's probably a better way to do your cascade of greps, too, such
> as building a file with one pattern per line and running a single
> "grep -f patternfile", but you haven't provided sufficient context.
>
The $grepstring itself can't have any surprises (famous last words) but
the entire construction is of course suspect. I see you're shrinking
the target of eval to the minimum, that seems sensible on first
principals but I can't think of a way to get the first pipe outside of
$grepstring. The thing is just to turn a string into a command because
the only way I know to create flexible commands is by way of strings.
Anyway, is there no way of putting a global protection on nasty
characters inside the list of directories? I'm sure I did read about
that, but there's so much to remember.
BTW I started doing this sort of cascading colorization with a very
similar function I use to explore the history. I can refine the search
with as many strings as I want, and they all show up with different
colors, which is even useful, not just eye candy. The final array gets
fed to Sebastian's 'n_list' for selection. Quite indispensable to me.
next prev parent reply other threads:[~2018-03-09 22:45 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-09 17:50 Ray Andrews
2018-03-09 20:23 ` Bart Schaefer
2018-03-09 22:45 ` Ray Andrews [this message]
2018-03-09 23:12 ` Ray Andrews
2018-03-10 3:20 ` Aaron Schrab
2018-03-10 3:51 ` Ray Andrews
2018-03-10 17:40 ` Ray Andrews
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=18dac66c-f348-8123-c051-4deb3dd21294@eastlink.ca \
--to=rayandrews@eastlink.ca \
--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).