LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   checking the number of parameters (https://www.linuxquestions.org/questions/programming-9/checking-the-number-of-parameters-599075/)

a.toraby 11-12-2007 11:30 AM

checking the number of parameters
 
hi
how to check the number of parameters that user passes to script. when number of them don't match to the desired number, script should be interupted.

colucix 11-12-2007 12:47 PM

$# stores the actual number of arguments passed to a script. You may check its value against a fixed one, with a simple if-then construct.

bulliver 11-12-2007 02:53 PM

Quote:

how to check the number of parameters that user passes to script. when number of them don't match to the desired number, script should be interupted.
Unless I've missed something, you have not mentioned which language your script is in. colucix's reply assumes Bash (and Perl ???)

In any case, almost every language stores the args in an array, of which you can easily check the length of. Be sure to subtract 1 from this if you are using a language that also stores the script name.

colucix 11-12-2007 03:34 PM

Quote:

Unless I've missed something, you have not mentioned which language your script is in. colucix's reply assumes Bash (and Perl ???)
Uh... yes, thanks for notice. Talking about scripts I assumed bash by default! ;)

a.toraby 11-13-2007 12:15 PM

oh my god. thanks for your notice. thats right. i forgot it ;)
i mean Korn shell scripting language.


All times are GMT -5. The time now is 04:04 AM.