Dynamic Variable in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic Variable in Linux
# 1  
Old 03-19-2013
Dynamic Variable in Linux

Hi All,

I am trying declare variables at runtime to use them for some calculation. What im doing is to try and run a bunch of sql queries in round-robbin fashion for few times. So im trying to create a different variable for each SQL file using the file name to keep track of the total time of individual queries.


Code:
for ((i=1;i<=$no_of_runs;i++))
do
	ls *.sql | while read SQL_FILE
	do
		start=`date +%s`;
		db2 -vtf $SQL_FILE > $SQL_FILE.out;
		end=`date +%s`;
		${SQL_FILE}_temptime=`expr $end - $start`;
		${SQL_FILE}_totaltime=`expr ${SQL_FILE}_totaltime + ${SQL_FILE}_temptime`;
	done	
done

Now i believe this is not allowed. ${SQL_FILE}_temptime is not the way to generate/use the variable. I think the way around this problem is to use "eval". I haven't used "eval" before and im having trouble putting it to use.
Can i request someone to guide me through this problem. If i have to use eval then how can it be used in my case.

Appreciate your help.

Thanks
A

Last edited by jim mcnamara; 03-19-2013 at 09:17 AM..
# 2  
Old 03-19-2013
try like this:

Code:
eval ${SQL_FILE}_temptime=`expr $end - $start`;
eval ${SQL_FILE}_totaltime=`expr ${SQL_FILE}_totaltime + ${SQL_FILE}_temptime`;

# 3  
Old 03-19-2013
Thanks for the reply Panyam.

I have already tried the approch mentioned by you and get the following error:

Code:
./Script.ksh[30]: eval: line 1: test.sql_temptime=1: no parent

It does calculate the time (1s) but gives this error as well.

A

Last edited by radoulov; 03-19-2013 at 10:52 AM..
# 4  
Old 03-19-2013
Since your intention is just to have the temptime and totaltime for individual SQL_FILE, you can use arrays instead: (Assuming you are using bash)

Code:
declare -A temptime totaltime
for ((i=1;i<=$no_of_runs;i++))
do
	ls *.sql | while read SQL_FILE
	do
		start=`date +%s`;
		db2 -vtf $SQL_FILE > $SQL_FILE.out;
		end=`date +%s`;
                temptime[$SQL_FILE]=`expr $end - $start`
                totaltime[$SQL_FILE]=`expr ${totaltime[$SQL_FILE]} + ${temptime[$SQL_FILE]}`

	done	
done

2 arrays, temptime and totaltime are declared as associative arrays. So, the array indexes will be the filenames, and the corresponding values will contain the duration taken to execute.(BTW, you do not need expr to do the calculation. Bash can do arithmetic on its own)

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 03-19-2013
To date I've seen no good reasons for wanting a dynamic variable name. How would you even use them later?

Arrays is much better yes, since otherwise, someone could insert a row containing `rm -Rf ~/` into your database and your code would execute it!
# 6  
Old 03-19-2013
Thanks Guru,

I am using ksh but yeah logic remains the same so it works.

Corona thanks for the tip. Will keep that in mind.

Cheers
ASmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic variable name in bash

Hello, I'm trying to build a small script to store a command into a dynamic variable, and I have trouble assigning the variable. #!/bin/bash declare -a var1array=("value1" "value2" "value3") var1arraylength=${#var1array} for (( i=1; i<${var1arraylength}+1; i++ )); do mkdir... (7 Replies)
Discussion started by: alex2005
7 Replies

2. Shell Programming and Scripting

Dynamic variable assignment

#!/bin/sh if then echo "Insufficient number of arguments ">> error".log" ; echo "Please check error log for more details"; exit 1 ; else file_name=$1".csv"; fi ; in_par_number=`head -n1 $file_name | sed 's///g' | wc -c` read -a arr <<< `head -n1 $file_name | sed 's// /g'` ... (6 Replies)
Discussion started by: JayDoshi
6 Replies

3. Shell Programming and Scripting

Dynamic variable assignment

My Code : -------------------------------------------- #!/bin/bash for i in `echo server1 server2` do eval ${i}_name = "apache" echo ${i}_name done -------------------------------------------- Current output : >./test.sh ./test.sh: line 5: server1_name: command not found... (3 Replies)
Discussion started by: sameermohite
3 Replies

4. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

5. Shell Programming and Scripting

Dynamic file name in variable

Hi guys. i have a requirment as below. I have a scripts which perform for loop for i in /backup/logs -- it will give all the logs file SC_RIO_RWVM_20120413064217303.LOG SC_RIO_RWXM_20120413064225493.LOG SC_RIO_RXXM_20120413064233273.LOG ... do open script.sh ---- in this file... (3 Replies)
Discussion started by: guddu_12
3 Replies

6. Shell Programming and Scripting

Help with Dynamic variable

I need some variable help TEMP1=Jane TEMP2=Sue X=1 eval USER=TEMP${X} echo $USER This gives output USER1 I would like to get Jane I have tried eval USER='TEMP${X}' eval USER="TEMP${X}" eval USER=`TEMP${X}` (3 Replies)
Discussion started by: Jotne
3 Replies

7. Shell Programming and Scripting

Dynamic variable assignment

Hi all, I’m very new to UNIX programming. I have a question on dynamic variable 1. I’m having delimited file (only one row). First of all, I want to count number of columns based on delimiter. Then I want to create number of variables equal to number of fields. Say number of... (5 Replies)
Discussion started by: mkarthykeyan
5 Replies

8. Shell Programming and Scripting

dynamic variable name

I found one post in another site with a solution for my problem the below solution should explain what I want. #!/bin/sh first="one" second="two" third="three" myvar="first" echo ${!myvar} But this gives error 'bad substitution' System info SunOS sundev2 5.9... (3 Replies)
Discussion started by: johnbach
3 Replies

9. UNIX for Dummies Questions & Answers

Dynamic variable values

Bit of a newbie :D with regard to unix scripting and need some advice. Hopefully someone can help with the following: I have a predefined set of variables as follows: AAA_IP_ADD=1.1.1.1 BBB_IP_ADD=2.2.2.2 I have a funnction call which retrieves a value into $SUPPLIER which would be... (3 Replies)
Discussion started by: ronnie_uk
3 Replies

10. Shell Programming and Scripting

Dynamic Variable Declatation

Evening all, Have been trying to create the following environment variable: ${MD_SYSTEM}_ZZ_EMAIL_SUPPORT="myname@domain.com" However when the script that contains the above is executed it returns: ksh: MDQA_ZZ_EMAIL_SUPPORT=myname@domain.com: not found Is what I'm trying to do... (2 Replies)
Discussion started by: Cameron
2 Replies
Login or Register to Ask a Question