Operating System - HP-UX
1831356 Members
3197 Online
110024 Solutions
New Discussion

Re: Range of positional parameters

 
SOLVED
Go to solution
Anthony deRito
Respected Contributor

Range of positional parameters

Silly question but I am in a hurry...
I want to set a variable based on a range of positional parameters instead of using:
VARIABLE="${2} ${3} ${4} ${5} ${6} ${7}"

So that the VARIABLE looks like:

echo $VARIABLE
one two three four five...

I do not know up front how many positional parameters are being passed to my script and that is why I need to use a range.
Thanks in advance!
5 REPLIES 5
RikTytgat
Honored Contributor
Solution

Re: Range of positional parameters

Hi,

If you want to skip a number of positional parameters, use 'shift' or 'shift n' to skip n positional parameters.

To assign the values of the other (remaining) positional parameters to one variable, do

VAR="$*"

This assigns all remaining positional parameters to VAR (separated by spaces)

Hope this helps,
Rik
Anthony deRito
Respected Contributor

Re: Range of positional parameters

Rik, thanks, this worked with the exception that I needed to do;

POS_PARAM_1="${1}"
shift
ALL_OTHER_PARAMS="${*}"

curt larson
Frequent Advisor

Re: Range of positional parameters

if your using a version of the ksh newer then 11/16/88

#!/usr/dt/bin/dtksh

var=${@:2} # all the positional parameters after the first
var=${@:2:4} # the four positional parameters starting with the second

the syntax is ${@:offset:length}

if length is not specified or greater then the number of parameters, the remaining parameters are used.
nobody else has this problem
Anthony deRito
Respected Contributor

Re: Range of positional parameters

Thanks Rik and Curt, your suggestions were both very helpfull. I appreciate your time.

Tony
CHRIS_ANORUO
Honored Contributor

Re: Range of positional parameters

Try using this in a script:
ms="command"
set -hk $ms; nn= `echo $#`; n=1
while [ $n -le $nn ]
do
me=command
ls -l $me
n=`expr $n+1`
shift 1
done
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.