LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2007, 11:30 AM   #1
a.toraby
Member
 
Registered: Sep 2006
Location: tehran
Distribution: open suse & open solaris
Posts: 102

Rep: Reputation: 15
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.
 
Old 11-12-2007, 12:47 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
$# 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.
 
Old 11-12-2007, 02:53 PM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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.
 
Old 11-12-2007, 03:34 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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!
 
Old 11-13-2007, 12:15 PM   #5
a.toraby
Member
 
Registered: Sep 2006
Location: tehran
Distribution: open suse & open solaris
Posts: 102

Original Poster
Rep: Reputation: 15
oh my god. thanks for your notice. thats right. i forgot it
i mean Korn shell scripting language.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C programming: Variable number of parameters to function kenneho Programming 8 03-21-2006 06:21 AM
checking available module parameters sensovision Linux - Newbie 2 10-28-2004 04:29 AM
Question about function with variable number of parameters xailer Programming 4 01-13-2004 11:37 AM
checking outgoing (masqueraded) ip-number of gatway from a lan client. eNTi Linux - Networking 1 12-10-2002 04:23 PM
Checking Software Version Number Ezekiel Linux - Newbie 1 04-24-2002 05:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration