Sujet : Re: Checking for right # of args in a shell script (Was: a sed question)
De : gazelle (at) *nospam* shell.xmission.com (Kenny McCormack)
Groupes : comp.unix.shellDate : 20. Dec 2024, 18:43:34
Autres entêtes
Organisation : The official candy of the new Millennium
Message-ID : <vk4ac6$1v4c6$1@news.xmission.com>
References : 1 2 3 4
User-Agent : trn 4.0-test77 (Sep 1, 2010)
In article <
vk43n0$3gtg6$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+
ng@hotmail.com> wrote:
...
Agreed, in general, but in practice, the need rarely arises.
>
I certainly disagree on this; if you have 10..19 (or 100..199 etc.)
arguments the '<' test just doesn't trigger but '-lt' does. I mean,
why use a wrong operator. If it will only in specific cases produce
correct results, or if it produced in most cases correct results;
it's just the wrong thing.
We're not talking about the same thing.
The idiomatic way to do this is just:
[ $# = 2 ] || usage()
>
Yes, but I don't use that but prefer (like you) [[...]], an in, say,
[ ] is easier in the simple cases. But, whatever, either way is fine.
Also, when I need to do more complex arg verification, I use bash's [[ ]]
mechanism (Yes, I know OP is using /bin/sh, but there is no reason nowadays
not to use bash).
>
If the OP is on Linux the 'sh' might actually be a Bash. If he's,
I assume Linux unless/until I hear otherwise. And I tend to also assume
some Debian-based Linux (again, unless/until ...). In Debian-based
Linuxes, sh is "dash", which is pretty much a minimal
subset/POSIX-compliant version of the shell. So, [[ ]] isn't available
there.
-- The randomly chosen signature file that would have appeared here is more than 4lines long. As such, it violates one or more Usenet RFCs. In order to remainin compliance with said RFCs, the actual sig can be found at the following URL: http://user.xmission.com/~gazelle/Sigs/GodDelusion