LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 05-06-2007, 02:03 PM   #1
Scott_82
LQ Newbie
 
Registered: May 2007
Posts: 4

Rep: Reputation: 0
HELP! Positional Parameters are giving me a headache!


I'm taking a class on Linux Shell Script Programming. In one of the scripts we have to write, it wants us to accept 5 positional parameters from the command line. It's really racking my brain. COULD SOMEONE PLEASE HELP ME!
 
Old 05-06-2007, 02:22 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, and welcome to LQ!

If you could formulate an actual question, maybe?


Cheers,
Tink
 
Old 05-06-2007, 02:25 PM   #3
card-suse
Member
 
Registered: Dec 2005
Location: Ohio, USA
Distribution: OpenSuSE 10.2
Posts: 74

Rep: Reputation: 15
What's the racking about? The positional parameters from the command line are passed into the shell through the variables $0, $1, $2, etc... where $0 is the shell script name and the others are the parameters you provided on the command line. So the five positional parameters in order are: $1, $2, $3, $4 and $5. If you want to assign them to a different variable name, then simply: my_variable=$1, etc... I'm assuming you are using BASH. Hope that helps...
 
Old 05-06-2007, 02:32 PM   #4
Scott_82
LQ Newbie
 
Registered: May 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Here's what the book is asking.

1. Create a script named Project5-2 in the $HOME/bin directory.
2. Insert a reference to the bash shell, appropriate comments, and your name as author.
3. Accept five positional parameters from the command line.
4. Display all five values on the screen
5. Display a usage clause if the incorrect number of values is entered.
6. Display an average of the five values.
7. Save the script, close it, and make it executable.

I've done the first two, that's simple enough, this is what I have

#!/bin/bash
#Name: Project5-2
#Author: Scott Adams

Then I get confused, I'm not sure how to make my script accept positional parameters. Can anyone tell me how?

I think the rest should be simple enough using echo and the usage is the if/fi command.
 
Old 05-06-2007, 03:42 PM   #5
card-suse
Member
 
Registered: Dec 2005
Location: Ohio, USA
Distribution: OpenSuSE 10.2
Posts: 74

Rep: Reputation: 15
Like I said Scott, they are already passed in automagically for you. Do me a favor and just add these lines to your script:

echo $0
echo $1

Now invoke your script at the command prompt:

<your script name here> 100

You should see the name of your Bash script echoed and 100. Get it?
 
Old 05-06-2007, 03:58 PM   #6
Scott_82
LQ Newbie
 
Registered: May 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks card,
I did that, and when I typed in

bash Project5-2 100

I got

Project5-2
100

I understand the concept of positional parameters, I just don't understand how to implement it the way the previous question wants. If you were given the problem previously stated, how would you do it? It's not that I want someone to do my work for me, but I'm self-teaching myself, and I need to see how it's done.

Thanks again.
 
Old 05-06-2007, 04:11 PM   #7
card-suse
Member
 
Registered: Dec 2005
Location: Ohio, USA
Distribution: OpenSuSE 10.2
Posts: 74

Rep: Reputation: 15
Here's are some of the best places to get all the info you could possibly want on Bash:

http://www.gnu.org/software/bash/manual/bashref.html
http://db.ilug-bom.org.in/Documentation/abs-guide/
http://wooledge.org/mywiki/BashFaq

Those should give you all the resources you need to finish that assignment in under 10 minutes, not including the reading of course. Still, you are about 1/2 way already since now you know how to pass in the five parameters at the command line and access them in your script. I'll give you one more piece of info: $# is the number of positional parameters passed into your script. So if you echo that in the last example you should see 1 because you passed in one positional parameter. Good luck Scott.

Also, we're all really self taught. Most of my grad professors stink and couldn't teach themselves out of a bag, so it's up to us to figure it out. Too bad really. I won't say that for every school I've been at since MIT was my best experience ever and the professors there rock. The place I'm at now is the pits. Again, good luck and I hope all the info above helps.
 
Old 05-06-2007, 04:32 PM   #8
Scott_82
LQ Newbie
 
Registered: May 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks card! Really helped out a lot. I'm still getting an error message, but I think I understand enough now. Thanks for all your help.
 
  


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
shell: positional parameters... neidias Programming 1 04-04-2005 03:45 PM
Positional Parameters (arguments) desbyleo Solaris / OpenSolaris 2 10-25-2004 12:51 PM
positional parameters $1 $2 in sed command Warmduvet Programming 12 09-22-2004 04:56 PM
Help with positional parameters Read_Icculus Linux - General 7 11-02-2003 05:23 AM
Need Help PORTFW is giving me a headache. cenci Linux - Networking 1 02-17-2003 07:08 AM


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

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