Re: variable substitution (269 Views)
Reply
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 1 of 8 (269 Views)
Accepted Solution

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
Honored Contributor
V. Nyga
Posts: 4,874
Registered: ‎09-20-2002
Message 2 of 8 (269 Views)

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 ***
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 3 of 8 (269 Views)

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"
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 8 (269 Views)

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...
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 5 of 8 (269 Views)

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.

Acclaimed Contributor
Dennis Handly
Posts: 25,071
Registered: ‎03-06-2006
Message 6 of 8 (269 Views)

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.
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 7 of 8 (269 Views)

Re: variable substitution

ok lots of examples ....

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

thanks

Chris.
hello
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 8 of 8 (269 Views)

Re: variable substitution

thanks all for the help and explanations ...

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

Chris.
hello
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.