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.
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.
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)
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)