| Author |
Message |
simargl

Joined: 11 Feb 2013 Posts: 416
|
Posted: 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.
Edited_times_total
|
|
Back to top
|
|
 |
SFR

Joined: 26 Oct 2011 Posts: 573
|
Posted: 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
|
|
 |
simargl

Joined: 11 Feb 2013 Posts: 416
|
Posted: Thu 14 Mar 2013, 12:18 Post_subject:
|
|
Thanks, shift 1 solved it!
|
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 841
|
Posted: 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
|
|
 |
simargl

Joined: 11 Feb 2013 Posts: 416
|
Posted: 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
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 841
|
Posted: 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
|
|
 |
|