0

This is my script script.sh:

Numbr_Parms=$#
a=`expr $Numbr_Parms - 2`
while [ $a -le $Numbr_Parms ]
do
    if [ "$a" =  "3" ]
    then
       PARAMSTRING="-param $3"
    else
       PARAMSTRING="$PARAMSTRING -param $a"
    fi
    a=`expr $a + 1`
done
echo $PARAMSTRING

Running:

script.sh  username pass p1=v1 p2=v2 p3=v3

Expected output:

-param p1=v1 -param p2=v2 -param p3=v3

But i am getting: $PARAMSTRING as

-param p1=v1 -param 4 -param 5

Not sure what is the Issue with $4 and $5

1
  • Would you consider rewriting your script by using getopt and handling your options and parameters in a slightly different way to take advantage of getopt ? Commented May 26, 2016 at 12:23

3 Answers 3

2

If you want to skip the first two positional parameters, just use

for arg in "${@:3}"; do
    PARAMSTRING+="-param $arg "
done

The right way to build up a sequence of parameters, though, is to use an array, which will work even if one of the arguments itself contains whitespace.

for arg in "${@:3}"; do
    PARAMS+=(-param "$arg")
done

mycommand "${PARAMS[@]}"
Sign up to request clarification or add additional context in comments.

3 Comments

You may want to add a trailing space; otherwise, strings will overlap.
True. Actually, that also makes me wonder why I'm not using an array like I should be.
Try this: set a b c d e; echo "${@:3}".
0

The problem is in the below line.

PARAMSTRING="$PARAMSTRING -param $a"

In this the $a does not means the positional argument. It is a variable it have the 4, 5, like that only.

If you print that variable you will not get the positional argument. You get only 4, 5, 6 only.

So, you have to get the positional argument using that variable a.

Try the below step it will work as you expect.

Numbr_Parms=$#
a=`expr $Numbr_Parms - 2`
while [ $a -le $Numbr_Parms ]
do
    if [ "$a" =  "3" ]
    then
        PARAMSTRING="-param $3"
    else
        eval pos=\$$a    # This is the indirect reference to get the positional argument from variable a.
        PARAMSTRING="$PARAMSTRING -param $pos"
    fi
    a=`expr $a + 1`
done

echo $PARAMSTRING

Comments

0

In:

PARAMSTRING="$PARAMSTRING -param $a"

You want to append the parameter in the position a. However, $a just holds the number. So to access $1, $2, etc you have to use variable indirection:

PARAMSTRING="$PARAMSTRING -param ${!a}"

The full script can be reduced to a simple loop over the parameters, using $# as limit:

for ((i=3; i<=$#; i++)); do
        PARAMSTRING="$PARAMSTRING -param ${!i}"
done
echo "$PARAMSTRING"

Or, even better, you can use an array like chepner shows in his answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.