Hello, On Fri, 17 Mar 2017, Marc Chamberlin wrote:
Bottom line is, the results of my doing this simple grep command exceeded my ability to easily grok why it failed. The error message is inadequate at helping me to understand what when wrong, especially when the very same command works in other directories just fine. That does implies inconsistent directory or file name or perhaps permissions or ??? some other related effects on the execution of the grep command, but wow, how is a poor user, who does not have a deep understanding of the internals of shell semantics or grep's algorithms suppose to figure this out?
Neither actually. Just some very basic facts about the shell and globbing. If you don't want to learn those, try using 'mc'. And actually, it _is_ rather simple: The shell expands the '*' and replaces the '*' with the result of the expansion. If that result happens to contain a string(!) (argument) that is an option for the command called, then, the command will take that string/argument as an option (unless a preceding argument was the special option '--' and the command has that '--' option). Again: use 'set -x' liberally if you don't understand something the shell is doing. After setting 'x' via 'set -x' each command executed is output before execution, prefixed with '+ '. E.g.: $ touch a b $ ls * a b $ touch ./-l $ ls * -rw-r--r-- 1 dh dh 0 Mar 18 16:19 a -rw-r--r-- 1 dh dh 0 Mar 18 16:19 b $ set -x $ ls * + ls -l a b -rw-r--r-- 1 dh dh 0 Mar 18 16:19 a -rw-r--r-- 1 dh dh 0 Mar 18 16:19 b $ rm ./-l + rm ./-l $ ls * + ls a b a b I could go into even further details (strace -eprocess ... ;), but the above should suffice. Once you've understood this, that the shell is just expanding and passing on the result, the behaviour of grep in this case becomes clear, as you've called it with the option '-.slice'. Again: Your $ grep run * in that dir (and not in other dirs) expanded to $ grep run ... -.slice ... and grep took the '-.' as an option. And to avoid that, use either '--' or './' or both, i.e. any of: $ grep run -- * $ grep run ./* $ grep run -- ./* Again: I recommend reading up on basic shell behaviour, esp. regarding "globbing" ;) Oh, and yes: with most GNU and other programs, options may appear anywhere on the command-line, which exactly is a feature of getopt and other option-parsing functions/libs that "bit" you in this case, so, a command: $ cmd foo -a b -c is the same as $ cmd -a -c -- foo b The latter is actually what 'getopt(3)' makes out of the former, IIRC (I'd have to check with strace and can't bother now). And having the '--' option is a consequence of this flexibility of allowing options anywhere instead of just between the command and the first non-option. Compare 'find' for a rather strict commandline- parsing. But there is an upside: you could touch a file ./-i in some directories to avoid an accidental 'rm * ~' instead of 'rm *~' ;) I don't recommend this though, better learn to be strict in "think (twice) before pressing enter"... Actually, after almost 20 years of using Linux, I still routinely use $ ls ... or $ echo rm ... before calling $ rm ... Just to be sure I've got the globbing and other stuff right. Remember: the shell and called commands do what you tell them to. It is _your_ responsibility what you call. Oh, but feel free to ask, once you're reading up on it and have trouble understanding this or that. HTH, -dnh -- Jeder hat das Recht auf seine eigene Meinung, aber er hat keinen Anspruch darauf, dass andere sie teilen. -- Manfred Rommel -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org