Max amount of parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Max amount of parameter
# 1  
Old 11-18-2017
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 advanceSmilieSmilie


Code:
#!/bin/ksh

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

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

# 2  
Old 11-18-2017
Code:
echo ${i}

# 3  
Old 11-18-2017
Thank you for your reply. I have done that, it returns 1,2,3...num but not the value of the parameter.
Code:
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

Quote:
Originally Posted by rdrtx1
Code:
echo ${i}


Last edited by Joveini; 11-18-2017 at 07:35 PM..
# 4  
Old 11-18-2017
You haven't said which version of ksh you're using. If it is a 1993 or later version, try:
Code:
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:
Code:
if [ "$num" -gt $# ]
then	printf 'Too many (%d) positional parameters found.  No more than %d expected.\n' $# "$num" >&2
	exit 1
fi


Last edited by Don Cragun; 11-18-2017 at 07:44 PM.. Reason: Add postscript.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-18-2017
Thank you very much. it works. your a starSmilieSmilieSmilieSmilie I'm using the latest version

Quote:
Originally Posted by Don Cragun
You haven't said which version of ksh you're using. If it is a 1993 or later version, try:
Code:
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:
Code:
if [ "$num" -gt $# ]
then	printf 'Too many (%d) positional parameters found.  No more than %d expected.\n' $# "$num" >&2
	exit 1
fi

# 6  
Old 11-18-2017
I'm glad it worked for you.

You could also try either of the following more direct approaches (without creating an auxiliary array):
Code:
	echo "${@:$i:1}"
	echo "${@:i:1}"

or, if you wanted to print the first $num positional parameters:
Code:
	printf '%s\n' "${@:1:num}"

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 11-19-2017
Why do you want to translate position numbers to values?
Normally you simply walk through the given values
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call Script with Parameter (that has another parameter)

Hi. How do I achieve this sh /EDWH-DMT02/script/MISC/exec_sql.sh "@/EDWH-DMT02/script/others/CSM_CKC/Complete_List.sql ${file_name}" Complete_List.txt The /EDWH-DMT02/script/MISC/exec_sql.sh has two parameters and it's working fine with this sh /EDWH-DMT02/script/MISC/exec_sql.sh... (7 Replies)
Discussion started by: aimy
7 Replies

2. Shell Programming and Scripting

Resolving a parameter which is passed as parameter

Hi, I have the following files. ->cat scr.sh export TMP_DIR=/home/user/folder1 export TMP_DIR_2=/home/user/folder2 while read line do cat "$line" done<file_list.dat ------------------------ -> cat file_list.dat $TMP_DIR/file1.txt $TMP_DIR_2/file2.txt --------------------------- -> cat... (6 Replies)
Discussion started by: barath
6 Replies

3. Shell Programming and Scripting

How to get the parameter value from the parameter file in perl?

hi all, i have a parameter file of following format, i want a method which can get the value of specific parameter. parameter file format: <Parameter Name="FileLocationWindows"> <Description> The directory location of the logger file. ... (1 Reply)
Discussion started by: laxmikant.hcl
1 Replies

4. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

5. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

6. UNIX for Dummies Questions & Answers

count amount of accounts?

Hi, I am relatively new to Unix and trying to understand as much as I can. I would like to know if it's possible to count the total number of Unix accounts? If so, can the count be done from any working directory or does it have to be specific to where the accounts are based? Thanks! (4 Replies)
Discussion started by: Trogan
4 Replies

7. Shell Programming and Scripting

Max amount of awk array indices

Does anyone know what the max amount of indices you can store in a awk array? (0 Replies)
Discussion started by: timj123
0 Replies

8. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies

9. Filesystems, Disks and Memory

max file parameter ( urgent )

I have digital UNIX V4.0d I want to change the parameter of maximum file becuse I face a problem while creating a new datafile in oracle 7.3.3 please advise Thanks (2 Replies)
Discussion started by: fmmq
2 Replies
Login or Register to Ask a Question