Live data from Hacker News

Unix Wildcards Gone Wild

defensecode.com

41–50 of 54 posts

Re: Unix Wildcards Gone Wild

#44
I remember when I used pdfimages to extract images from a pdf file and I didn't read the manual beforehand. It turned out that you should call it like pdfimages and if you don't specify a prefix it generates filenames in the form of -img001, -img002, ... (or something like that). I had a hard time deleting those images.

Re: Unix Wildcards Gone Wild

#46

Tangentially, anyone know how to make zsh less greedy about parsing wildcards? Something like this will fail with "no files matched", and the command won't run: rsync example.com:/foo/* . My workaround is to quote the argument, but it's annoying.

IIRC zsh has an option for this (if a wildcard has no matches, it just gets left in its original form), but I don't remember what.

There's also `noglob`, which disables glob expansion for a command (e.g. `noglob echo 3*4 | bc`).

Re: Unix Wildcards Gone Wild

#47
post #46

Tangentially, anyone know how to make zsh less greedy about parsing wildcards? Something like this will fail with "no files matched", and the command won't run: rsync example.com:/foo/* . My workaround is to quote the argument, but it's annoying.

IIRC zsh has an option for this (if a wildcard has no matches, it just gets left in its original form), but I don't remember what. There's also `noglob`, which disables glob expansion for a command (e.g. `noglob echo 3*4 | bc`).

Unfortunately, this has some unwanted side effects (see https://github.com/robbyrussell/oh-my-zsh/issues/2945). The noglob option might work, if it doesn't prevent scp from working with local wildcards.

Re: Unix Wildcards Gone Wild

#48
Thankfully on BSD the options are passed before the file names.

For example chown username:username files directory -R

Doesn't actually work. You have to move the -R to before the usernames. chown -R username:username files directory.

Same thing with rm.

Re: Unix Wildcards Gone Wild

#49

David A. Wheeler, FOSS (and occasionally security) luminary, who also happens to be creator of the popular sloccount tool, has an excellent page that covers this topic and how to use paths safely and portably in shell scripts (spoiler: it's hard ) : http://www.dwheeler.com/essays/filenames-in-shell.html I strongly recommend his many other essays to HN readers: http://www.dwheeler.com/ Edit: a simple way to avoid thes…

> portably in shell scripts (spoiler: it's hard ) At what point do you give up and, if you must have portability, bootstrap a saner environment? Are there many options in this area? Assume perl is available? Use autoconf-like shell script compiler? Starting with Lua (which I hope/assume can build anywhere, though I know it's missing lots of functionality out of the box)?

I'm lucky not to have to worry about this, but I figured that having Perl is a safe assumption for desktop and server Unix systems.

Not embedded devices, but I guess those are different enough that you're probably not targetting them for portable scripts. Oh no, have I contradicted myself? :)

Re: Unix Wildcards Gone Wild

#50
post #11

Many people are recommending the '--' option-terminating option. Note David Wheeler's caution (in the essay already linked by AceJohnny2 at https://news.ycombinator.com/item?id=8190208 ) about why this is not an all-purpose solution: http://www.dwheeler.com/essays/filenames-in-shell.html#dashd... .

The reasons given there aren’t really any good: 1. For “--” to work, all maintainers would have to faithfully use “--” in practically every command invocation. That just doesn’t happen in real life, even after decades of people trying. People forget it all the time; no one is that consistent, especially since code seems to work without it. Very few commands require it, after all. So because other people may or may no…

> So because other people may or may not forget it, I shouldn’t use it in my scripts/day-to-day usage?

He specifically mentions that that is not his point, but rather that he is arguing against exactly the sort of "just use '--'" response that one can find in this post:

> Do feel free to use “--” between options and pathnames, when you can do it, as an additional protective measure. But using “--” as your primary (or only) mechanism for dash-prefixed filenames is bad idea.

Post reply on HN