Shell script to create runtime variables based on the number of parameters passed in the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to create runtime variables based on the number of parameters passed in the script
# 1  
Old 05-25-2016
Shell script to create runtime variables based on the number of parameters passed in the script

Hi All,

I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them

Code:
abc.sh
----------
export Numbr_Parms=$#
export a=1
while [ $a -le $Numbr_Parms ]
do
export DBName_$a="$"$a
echo DBName_$a
echo $DBName_$a
a=`expr $a + 1`
done

The output this code gives is as follows :
Code:
abc.sh x y z
 
DBName_1
1
DBName_2
2
DBName_3
3


while the expected output is :

Code:
DBName_1
x
DBName_2
y
DBName_3
z


Thanks in advance.

Regards,
Dev
# 2  
Old 05-25-2016
Code:
export Numbr_Parms=$#
export a=1
while [ $a -le $Numbr_Parms ]
do
eval export DBName_$a=\$$a
echo DBName_$a
eval echo \$$DBName_$a
a=`expr $a + 1`
done

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 05-25-2016
As you can see, you have to go the extra mile to achieve what you intended, and the eval has some caveats tagged to it. In recents shells, this could be done way easier using arrays:

Code:
DBname=($@)
for i in ${!DBname[@]}
  do    echo $i, ${DBname[$i]}
  done
0, x
1, y
2, z

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 06-04-2016
Thanks Rudic, worked for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting number of argument passed to a shell script

Hi Experts, I have been trying to work on a simple shell script that will just add the two argument passed to it. Here is what i tried : #!/bin/bash welcome(){ echo "Welcome to this Progg. which will accept two parameter" } main_logic(){ arg=$# echo "Number of argument passed is... (4 Replies)
Discussion started by: mukulverma2408
4 Replies

2. Shell Programming and Scripting

Three parameters passed from a script to other at different time

nothing (0 Replies)
Discussion started by: shikha84
0 Replies

3. Programming

create a spool file based on values passed from korn shell to sql script

this is my issue. 4 parameters are passed from korn shell to sql script. parameter_1= varchar2 datatype or no value entered my user. parameter_2= number datatype or no value entered my user. parameter_3= number datatype or no value entered my user. parameter_4= number datatype or no... (5 Replies)
Discussion started by: megha2525
5 Replies

4. UNIX for Dummies Questions & Answers

Create a shell script for write files with 2 parameters

Hello, I'm a newbie in shell script. So, i would like to create a shell script which take 2 IN parameters (PARAM1 and PARAM2). This script need to create 2 files as : I need to create this file /etc/apache2/sites-available/PARAM2 : <VirtualHost *:80> DocumentRoot "/home/PARAM1/www"... (0 Replies)
Discussion started by: chatlumo
0 Replies

5. Shell Programming and Scripting

Dynamic SQL query based on shell script parameters

Hi, I need a script that will run a dynamic Oracle SQL. Dynamic meaning the SQL statement depends on the parameter. For instance, something like this: #!/bin/ksh -x # Set environment . /home/mine/set_vars sqlplus $LOGINID <<! >> /home/mine/log.txt select count(1) from $1 where... (2 Replies)
Discussion started by: laiko
2 Replies

6. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

7. UNIX for Dummies Questions & Answers

ksh: verifying number of parameters passed

hi all, i have a ksh script that takes up to 3 parameters -- only 2 of which are required. what's the simplest way to check if the user passed 2 or 3 parameters? if 3 parameters are not null then do this elif 2 parameters are not null then do this else echo "you need at least 2... (5 Replies)
Discussion started by: ankimo
5 Replies

8. Shell Programming and Scripting

Problem with script not able to take parameters passed to it

debug output: (3 Replies)
Discussion started by: dsravan
3 Replies

9. Shell Programming and Scripting

Problem with script not able to take parameters passed to it

when I pass any 2 parameters it always says: Number of parameters passed: 1 and the job_name as x Can some body help? (7 Replies)
Discussion started by: dsravan
7 Replies

10. Shell Programming and Scripting

Number of parameters to a shell script

Is there any restriction on number of parameters can be passed on to the shell script? I found, after 9th parameter for parameter 10, it is taking parameter 1. (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question