Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 18 Jun 2013, 02:50
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Problem with positional parameters (solved)
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [6 Posts]  
Author Message
simargl


Joined: 11 Feb 2013
Posts: 414

PostPosted: Thu 14 Mar 2013, 08:56    Post subject:  Problem with positional parameters (solved)  

Im spkg package manager I have functions for installing and removing packages

Code:
install_package() {
.........
}
remove_package() {
..........
}


At the end of that script among others are these two
Code:
case "$1" in

  install|-i)
     PKG="$@"
     install_package ;;
   
  remove|-r)
     PKG="$@"
     remove_package ;;

esac


Problem is that -i (or -r) are picked as part of $PKG variable and returned error is "-i does not exist"

I can't find solution for PKG to be $2 $3... and anything after that (Not $1, because it is taken).

I hope you could understand this.

Last edited by simargl on Thu 14 Mar 2013, 13:24; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website 
SFR


Joined: 26 Oct 2011
Posts: 573

PostPosted: Thu 14 Mar 2013, 10:27    Post subject: Re: Problem with positional parameters  

How about this:
Code:
case "$1" in

  install|-i)
     shift
     PKG="$@"
     install_package ;;
   
  remove|-r)
     shift
     PKG="$@"
     remove_package ;;

esac

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send private message 
simargl


Joined: 11 Feb 2013
Posts: 414

PostPosted: Thu 14 Mar 2013, 12:18    Post subject:  

Thanks, shift 1 solved it! Razz
Back to top
View user's profile Send private message Visit poster's website 
seaside

Joined: 11 Apr 2007
Posts: 841

PostPosted: Thu 14 Mar 2013, 12:37    Post subject:  

simargl,

PKG="$2" will set the second command line parameter.
"$@" means the entire command line arguments.

Cheers,
s
(I guess you could mark this thread "Solved")
Back to top
View user's profile Send private message 
simargl


Joined: 11 Feb 2013
Posts: 414

PostPosted: Thu 14 Mar 2013, 13:23    Post subject:  

seaside wrote:
PKG="$2" will set the second command line parameter.
"$@" means the entire command line arguments.

I know that, but needed was $2 and everything after ("$@" would take $1 also), and Yes sfr solved it!
Back to top
View user's profile Send private message Visit poster's website 
seaside

Joined: 11 Apr 2007
Posts: 841

PostPosted: Thu 14 Mar 2013, 17:01    Post subject:  

simargl wrote:
seaside wrote:
PKG="$2" will set the second command line parameter.
"$@" means the entire command line arguments.

I know that, but needed was $2 and everything after ("$@" would take $1 also), and Yes sfr solved it!

simargl,

Yes, I see now, you wanted to process an unknown quantity of command line parameters (packages) after determining the first parameter (-i or -r). "Shift" commnand is the way to do that as it allows a repeated use of $1 in a while loop.

Cheers,
s
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [6 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0547s ][ Queries: 12 (0.0153s) ][ GZIP on ]