* Bad math expression due to parameters @ 2005-09-02 23:16 DervishD 2005-09-03 3:53 ` Bart Schaefer 0 siblings, 1 reply; 3+ messages in thread From: DervishD @ 2005-09-02 23:16 UTC (permalink / raw) To: Zsh Users Hi all :) I have this in a script, after parsing command line arguments: [[ $OPTARG -lt 8 ]] && OPTARG=8 If I call the script like this: $ ./script -o -- then $OPTARG is "--" and that gives a "bad math expression" in the test above. How can I solve this? Must I validate $OPTARG before messing with it on a math expression? Is this validation enough?: [[ "OPTARG" = [[:digit:]]## ]] Can I do a more clever validation? Thanks a lot in advance :) Raúl Núñez de Arenas Coronado -- Linux Registered User 88736 | http://www.dervishd.net http://www.pleyades.net & http://www.gotesdelluna.net It's my PC and I'll cry if I want to... ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Bad math expression due to parameters 2005-09-02 23:16 Bad math expression due to parameters DervishD @ 2005-09-03 3:53 ` Bart Schaefer 2005-09-03 8:28 ` DervishD 0 siblings, 1 reply; 3+ messages in thread From: Bart Schaefer @ 2005-09-03 3:53 UTC (permalink / raw) To: Zsh Users On Sep 3, 1:16am, DervishD wrote: } } [[ $OPTARG -lt 8 ]] && OPTARG=8 } } [when] $OPTARG is "--" and that gives a "bad math expression" in } the test above. } } How can I solve this? Must I validate $OPTARG before messing with } it on a math expression? Either that, or cheat by not using math at all. [[ $OPTARG = <-7> ]] && OPTARG=8 However, I would think you want to treat "--" as a special case before you get as far as doing other stuff with OPTARG. ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Bad math expression due to parameters 2005-09-03 3:53 ` Bart Schaefer @ 2005-09-03 8:28 ` DervishD 0 siblings, 0 replies; 3+ messages in thread From: DervishD @ 2005-09-03 8:28 UTC (permalink / raw) To: Bart Schaefer; +Cc: Zsh Users Hi Bart :) * Bart Schaefer <schaefer@brasslantern.com> dixit: > On Sep 3, 1:16am, DervishD wrote: > } [[ $OPTARG -lt 8 ]] && OPTARG=8 > } > } [when] $OPTARG is "--" and that gives a "bad math expression" in > } the test above. > } > } How can I solve this? Must I validate $OPTARG before messing with > } it on a math expression? > Either that, or cheat by not using math at all. > > [[ $OPTARG = <-7> ]] && OPTARG=8 Very GOOD trick, Bart :) As always, I'm not aware of the pattern comparisons. I use them sometimes, but... > However, I would think you want to treat "--" as a special case before > you get as far as doing other stuff with OPTARG. It is treated specially in the script, but the problem is that if the caller passes '--' just after an option which requires an argument, it is consumed as the argument :( If I have to do special processing of every option and argument, I better get rid of getopts and do the processing by hand. I prefer to eat '--' in OPTARG, test if it is a valid argument for the option and issue a fail. Anyway you're right, I should 'search' for '--' just before doing any processing (just the same I search for '--help' at the very beginning of the script) and pass 'getopts' only the set of options before the '--', while consider the others as non options (filenames in this particular case). As far as I know, the SUS standard require 'getopts' to treat '--' specially as marking the end of options Doing the job by hand is something I've been considering, because that way I will have full control, and the only thing I loose is the ability of doing "script -xyz" meaning "script -x -y -z" (I can implement that, but I'm not sure if it is worth the effort). Thanks for all your help Bart :) Raúl Núñez de Arenas Coronado -- Linux Registered User 88736 | http://www.dervishd.net http://www.pleyades.net & http://www.gotesdelluna.net It's my PC and I'll cry if I want to... ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-09-03 8:24 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2005-09-02 23:16 Bad math expression due to parameters DervishD 2005-09-03 3:53 ` Bart Schaefer 2005-09-03 8:28 ` DervishD
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).