LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-02-2015, 03:31 PM   #1
vamshi krishna
LQ Newbie
 
Registered: Jul 2015
Posts: 3

Rep: Reputation: Disabled
Red face How do I pass multiple parameters? Is there a possibility?


So I'm a newbee to Unix Programming.
I've been reading the unix shell programming by Kochen & Wood lately. I know how to pass parameters in command line.
Example: sh display.sh vamshi krishna


Code:
echo " your name is $1 $2 "

But what if write a code using functions to do an operation and we dont know how many arguments can be passed by the user.

Example: the user wants to add 2 numbers at a time.
. add.sh
sum 5 6
This should give him 11 as output



Now what if the user wants to add 4 numbers?
example: add.sh
sum 1 2 3 4
This should give the output 10


So is there a way we can do addition of any number of numbers with a single program? Help will be appreciated

Thanks

Last edited by vamshi krishna; 07-02-2015 at 03:32 PM.
 
Old 07-02-2015, 03:42 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Look in your book for special characters. Currently you have used '$' to return the value of a single variable / parameter. There will be a section explaining what special character contains
all parameters passed to a script / function.
 
Old 07-02-2015, 04:28 PM   #3
vamshi krishna
LQ Newbie
 
Registered: Jul 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
@grail I have read that section already. I didnt find anything useful. Thats why I've posted it here.
can a function accommodate single or multiple augments? Is it really possible??

sh add.sh
sum 4 54 34 234 645 234 234
Possible???
 
Old 07-02-2015, 04:43 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
$1 will get you the first argument, $2 will get you the second, etc.
$# will tell you how many arguments there are
$@ will grab all of the arguments

You can also use "shift" to shift the arguments by one, eg:
echo $1
shift
echo $1
shift
echo $1

Will print out the first three arguments.

Stick it in a while loop using $# and keep adding $1 to the sum and you can add as many numbers as you want.
 
1 members found this post helpful.
Old 07-02-2015, 10:00 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Information provided above is what you need and if the book you have has nothing about $@ in it I would suggest getting a new book as that one is flawed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to pass kernel parameters. balakasaiah Linux - Kernel 8 01-13-2015 05:54 AM
run CGI and pass GET parameters extasic Programming 6 10-17-2008 09:53 AM
How to call the modules and pass parameters to it? shendeswanand Linux - Software 1 12-31-2005 10:18 PM
How to pass on boot parameters Slack 10.1 tdking19 Slackware 3 03-21-2005 11:41 AM
howto pass parameters to modules Sammy2ooo Linux - Software 5 07-01-2003 09:30 PM


All times are GMT -5. The time now is 09:15 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration