1753719 Members
5878 Online
108799 Solutions
New Discussion юеВ

variable substitution

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

variable substitution

Hi,

I am trying to set a variable and run a test on it later but m struggling a bit:

count=0
for data in 1 2 3 4
do
count=$((count+1))

if [[ ! -f /tmp/test ]] ; then

diafail$count=1

fi

I basically want diafial$count to be tested later

so

if [[ diafail$count -ne 1 ]] ; then

etc etc

but this isnt working

any ideas?

thanks

chris
hello
7 REPLIES 7
V. Nyga
Honored Contributor

Re: variable substitution

Hi,

first, i would do some 'echo's in this script.
For example 'echo $count'
Then, I don't know if 'count=$((count+1))' works like you want.
Maybe 'count=(($count+1))'?

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
Peter Nikitka
Honored Contributor
Solution

Re: variable substitution

Hi,

in this case 'eval' is your friend or change to use arrays. I set test 1+3 in my example.

touch /tmp/test1 /tmp/test3
cat /tmp/chtst
#!/usr/bin/ksh
typeset -i count=0 max=4

while [ count+=1 -le max ]
do
if [ -f /tmp/test$count ]
then eval diafail$count=1
else eval diafail$count=0
fi
done

count=0
while [ count+=1 -le max ]
do
if eval [ \$diafail$count -eq 1 ]
then print test $count active
fi
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: variable substitution

Hi Chris:

I'd use an array of variables:

# cat ./myscript
#!/usr/bin/sh
set -A diafail
count=0
for data in 1 2 3 4
do
count=$((count+1))

if [[ ! -f /tmp/test ]] ; then
diafail[$count]=1
else
diafail[$count]=0
fi

if [[ ${diafail[$count]} -ne 1 ]] ; then
echo "OK"
else
echo "BAD"
fi
done

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: variable substitution

Since you are creating variable names on the fly, you want the shell to evaluate variables first, then parse the line, and as mentioned, the eval statement is the secret. eval says: take everything on the current line, evaluate all the variables then execute the line. This illustrates the technique:


for COUNT in 1 2 3 4
do
eval "VAR$COUNT=$COUNT"
eval "print VAR$COUNT=\$VAR$COUNT"
done

Now a bit of explanation is needed so you turn on shell tracing like this:

set -x
+ eval VAR1=1
+ VAR1=1
+ eval print VAR1=$VAR1
+ print VAR1=1
VAR1=1
+ eval VAR2=2
+ VAR2=2
+ eval print VAR2=$VAR2
+ print VAR2=2
VAR2=2
+ eval VAR3=3
+ VAR3=3
+ eval print VAR3=$VAR3
+ print VAR3=3
VAR3=3
+ eval VAR4=4
+ VAR4=4
+ eval print VAR4=$VAR4
+ print VAR4=4
VAR4=4

So you see that $COUNT must first be turned into a simple character, then the entire line is evaluated. In the print statement, we escape the $ in the string $VAR$COUNT so that the $ remains but $COUNT gets turned into a number.

This coding would be a bit easier to understand if you used an array:

set -A DIAFAIL 0 0 0 0 0
for COUNT in 1 2 3 4
do
if [[ ! -f /tmp/test$COUNT ]]
then
DIAFAIL[$COUNT]=1
fi
done
echo ${DIAFAIL[@]}

In the above example, the DIAFAIL array tests for /tmp/test1 then test2, test3 and finally test4. If any of them are not files, the corresponding array location will be 1, otherwise 0. Note that arrays always start at 0 so there will be a DIAFAIL[0] value printer with the [@] shorthand.



Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: variable substitution

>Volkmar: I don't know if 'count=$((count+1))' works like you want.

This works. But if you want count to be an integer you could use (()) for arithmetic expressions:
(( count = 0 ))
(( count += 1 ))

>Maybe 'count=(($count+1))'?

This doesn't return a value and gets a syntax error.
lawrenzo_1
Super Advisor

Re: variable substitution

ok lots of examples ....

I'll give them a whirl and let you know.

thanks

Chris.
hello
lawrenzo_1
Super Advisor

Re: variable substitution

thanks all for the help and explanations ...

Array apears to be the best option for my task .....

Chris.
hello