I imagine the problem with this script is when assigning the dynamic variables
Code:
"$var2Value01=$(command 01)"
and
Code:
"$var2Value02=$(command 02)"
This is correct, but maybe in a different way than you may have thought.
The shell parses the commandline in several distinct steps and one of these steps is the "expansion" of variables - that is, replacing them with their values. The problem is that all variables are expanded at the same time.
Therefore you need eval, because your construct implies a "second pass of expansion" to be applied onto your line. Unfortunately you cannot selectively restart a certain parsing step. You can only restart the whole process from the beginning. Therefore you perhaps need - to make your line work - to distinguish between the parts that are meant to be expanded in the first pass and the ones meant to be expanded in the second one. Notice that the shell "consumes" some characters and if you need to have them still there you need to escape them properly so that they are not consumed in the first pass:
Code:
$ echo "two words"
two words
$ echo \"two words\"
"two words"
$ eval echo "two words"
two words
$ eval echo \"two words\"
two words
$ eval echo \\"two words\\"
two words
$ eval echo \\\"two words\\\"
"two words"
To really understand all the differences between these lines pipe the echo-output into wc -w to count the words for every command variation.
A word about eval in general: stay away from it if you can! Most times you can find a solution where you do it differently and you don't need eval at all. This is (in almost every case) preferable, because eval is very dangerous. It is very easy to lose track of what really gets executed and why. Only if there is definitely no other way then use it as a last resort.
I hope this helps.
bakunin
These 2 Users Gave Thanks to bakunin For This Post:
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)
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)
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)
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)
QUERY IN BRIEF
Listing the query in short
#! /bin/csh -f
#say i have invoked the script with two arguments : a1 and 2
set arg = $1 # that means arg = a1
echo "$arg" #it prints a1
#now what i want is:
echo "$a1"
#it will give error message :a1 undefined.
#however what i need is that the... (2 Replies)
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)
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... (5 Replies)
Hello,
I have a problem with a bash script, I've been doing recherches but i can't make it work. It is my first time with a dynamic variable and i don't understand how to write it.
#!/bin/bash
USER=BARSPIN
HOTKEYS_PATH="/home/$USER/Documents/bash/tibia/HOTKEYS"
CFG1="A"
CFG2="B"... (5 Replies)
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)
I am trying to create some variables based on the input by the user, say if user entered 3 then 3 variables and if 5 then 5 variables.
I am using a for loop
for (( i=1; i <= $num; i++ ))
do
x="num"
x+=$i
done
When i am using echo $x it will show num1 but now how to create variables... (3 Replies)