LinuxQuestions.org
Review your favorite Linux distribution.
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 10-19-2010, 11:25 AM   #1
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Rep: Reputation: 0
Question Shifting positional parameter inside a function


I need to shift the positional parameters of a script inside a function, but any call to "shift 1" inside a function shifts only the parameters of that function. Is there some way of
accomplishing that?

I tried another approach using an alias. The problem is that I have to take the result of the alias. So I call in my script:

Code:
var=$(shiftalias)
At first time, it works correctly, but after that it does not shift the parameters anymore. Does Someone know why?

Thanks.
 
Old 10-19-2010, 12:02 PM   #2
mitchpotter
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Rep: Reputation: 0
Maybe a little more detail would be helpful. What exactly are you trying to do?

How are you using the alias?

Also you might consider that using shift inside the function is shifting the function's copies of the parameters, not the actual global values.
 
Old 10-19-2010, 12:06 PM   #3
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Original Poster
Rep: Reputation: 0
I figured out why the call $(shiftalias) doesn't work. The call is executed in a subsheel, so it does not interfere in the script.

But I still need to know some way to change the script parameters inside a function.
 
Old 10-19-2010, 12:12 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
From what I read in the Bash manpage, there is no way (documented) to do this. When in a function, the positional params are replaced with those of the function, if any. `shift` then applies to them. That's apparently the end of story (manpage version anyhow).

However, (assuming the above is correct) is there no possible way to re-write the code so that you can work around this? If the script isn't gigantic, would it maybe help if you posted it so that someone may see and suggest a way to get around the issue?
 
Old 10-19-2010, 12:13 PM   #5
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by mitchpotter View Post
Also you might consider that using shift inside the function is shifting the function's copies of the parameters, not the actual global values.
Yes I know, but I want to know if there is some way to do that.
 
Old 10-19-2010, 12:36 PM   #6
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by GrapefruiTgirl View Post
From what I read in the Bash manpage, there is no way (documented) to do this. When in a function, the positional params are replaced with those of the function, if any. `shift` then applies to them. That's apparently the end of story (manpage version anyhow).

However, (assuming the above is correct) is there no possible way to re-write the code so that you can work around this? If the script isn't gigantic, would it maybe help if you posted it so that someone may see and suggest a way to get around the issue?
What i'm trying to do is a function like getopts (with some improvements). There are other ways to do what I want to do, but handling the parameters inside the function would be best way. But i'm convinced that there is no way to do that.
 
Old 10-19-2010, 12:44 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Before you get into the function call, stick the script's parameters into an array. Once you are inside the function, manually shift the array elements using a loop. Here's an example:

First copy your positional parameters ($1, $2, etc.) into an array called "blah":
Code:
blah=( $(echo $*) )
Then, inside the function, you need to shift them:
Code:
# code to shift blah[] to the left by one:

for i in $(seq 0 $((${#blah[*]}-2))); do blah[$i]=${blah[i+1]}; unset blah[i+1]; done
So every time you do that loop, blah[] all shifts left by one.

Maybe there's another way- but this works.
 
Old 10-19-2010, 12:46 PM   #8
mitchpotter
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Rep: Reputation: 0
That's a good solution and exactly what I would have done.
 
Old 10-19-2010, 12:55 PM   #9
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by GrapefruiTgirl View Post
Before you get into the function call, stick the script's parameters into an array. Once you are inside the function, manually shift the array elements using a loop. Here's an example:

First copy your positional parameters ($1, $2, etc.) into an array called "blah":
Code:
blah=( $(echo $*) )
Then, inside the function, you need to shift them:
Code:
# code to shift blah[] to the left by one:

for i in $(seq 0 $((${#blah[*]}-2))); do blah[$i]=${blah[i+1]}; unset blah[i+1]; done
So every time you do that loop, blah[] all shifts left by one.

Maybe there's another way- but this works.

That would be a good solution, but the problem is I do not want to worry about the function outside it. The function will be used in many scripts and I do not want to have to call any code every time I use the function.
 
Old 10-19-2010, 01:03 PM   #10
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Hmm.. Well, going with this same idea, my only further suggestion would be to have the array assignment code I put above, plus the function itself which contains the shifting loop, into a single file which gets sourced into any other scripts that need the function. So, when you source the file, automatically blah[] gets assigned the values of $1 $2 etc and the function contains the loop.

I have no other idea - maybe someone else can give a different idea, or you'll just need to do some more working around.

Good luck with this; keep us posted as to what you come up with for a solution.
 
  


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
function parameter vbx_wx Programming 5 04-04-2010 02:34 PM
Compilation issue when Function is parameter in function call on LINUX sa20358 Linux - Software 2 07-24-2008 10:19 PM
what is function parameter? Jeon, Chang-Min Linux - Software 2 06-22-2006 06:53 PM
trouble with positional parameter $0 on bash. linux distro: ubuntu breezy linrookie Linux From Scratch 2 05-07-2006 12:22 PM
Bash function parameter Misel Programming 2 05-17-2003 11:51 AM


All times are GMT -5. The time now is 04:25 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