Max amount of parameter

Hello everyone,

I am trying to write a ksh script to accept user amount of parameters. so far I have written the following but, when I run the script it gives me the "i" back instead of the parameter's value. can anyone please tell me what I'm doing wrong.
Thank you in advance:):slight_smile:

#!/bin/ksh

echo "How many Parameter would you like to accept?"
read num

for ((i=1;i<=num;i++))
do
echo ${!i}
done

echo ${i}

Thank you for your reply. I have done that, it returns 1,2,3...num but not the value of the parameter.

xxx@xxx-xxxxxxxx:~/task4$ ksh iii.ksh 7 8 9
How many parameter would you like to calculate?
10
1
2
3
4
5
6
7
8
9
10

You haven't said which version of ksh you're using. If it is a 1993 or later version, try:

ARGV=($0 "$@")	# Put this before your loop.
 ... ... ...
	echo "${ARGV[$i]}"
 ... ... ...

Note that you'll need to reassign the ARGV array after any calls to shift or set (when set is used to add or replace positional parameters).

PS: Note that if you're trying to determine if your script was called with more than $num positional parameters, the way to test for that is with:

if [ "$num" -gt $# ]
then	printf 'Too many (%d) positional parameters found.  No more than %d expected.\n' $# "$num" >&2
	exit 1
fi
1 Like

Thank you very much. it works. your a star:):):):smiley: I'm using the latest version

I'm glad it worked for you.

You could also try either of the following more direct approaches (without creating an auxiliary array):

	echo "${@:$i:1}"
	echo "${@:i:1}"

or, if you wanted to print the first $num positional parameters:

	printf '%s\n' "${@:1:num}"
1 Like

Why do you want to translate position numbers to values?
Normally you simply walk through the given values

echo "here are the $# values:"
for arg
do
  echo "$arg"
done

for arg is short for for arg in "$@" .
If you want to associate positions with the values then you can store the values in an array, as Don showed.

Shouldn't that read if [ "$num" -lt $# ] ?

1 Like

I am trying to calculate the total of the parameters that is set by the user input and get the average of those parameters. That's why I need the values rather than the position numbers. It is just part of my coursework as a student.

Thank you Don Cragun, the direct approach is much better than creating a new variable.

PS. I would very much appreciate any links or book recommendation to understand ksh script.

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.