Bash - I'm sure this is possible, but how?
In this example:
Code:
t=60
for i in t; do
if [ \$$i -eq 60 ]; then
echo "the number is $i"
else
echo fail
fi
done
This results is an error "Integer expression expected" The if statement is comparing the literal string of "$t" to 60 and obviously equates to false, how can i get bash to compare the result of the variable and compare 60 to 60 and equate to true,
Thanks for any help
Mark
Re: Bash - I'm sure this is possible, but how?
Hi CaptainMark.
Just remove the '\$':
Code:
...
if [ $i -eq 60 ]; then
...
Let us know how it goes.
Regards.
Re: Bash - I'm sure this is possible, but how?
I think maybe another problem is
Code:
t=60
for i in t; do
which means the [ $i -eq 60 ] is trying to test the string value "t" instead of the integer value of variable t ("$t") i.e.
Code:
$ t=60; for i in t; do echo $i; if [ $i -eq 60 ]; then echo "True"; else echo "False"; fi; done
t
bash: [: t: integer expression expected
False
$
$ t=60; for i in "$t"; do echo $i; if [ $i -eq 60 ]; then echo "True"; else echo "False"; fi; done
60
True
What is the purpose of the for .. in construct here?
Re: Bash - I'm sure this is possible, but how?
It's not clear what you are wanting OP, but I suspect you are trying to work out how to use indirect variables in bash? E.g:
Code:
$ t=60
$ i=t
$ echo ${!i}
60
Here, ! is the bash indirection expansion operator.
Re: Bash - I'm sure this is possible, but how?
I figured it out eventually, I simply wasn't using the correct terminology, once I discovered the term 'recursive variable substitution' I was away, I made use of the eval command as seen in my other post here http://ubuntuforums.org/showthread.php?t=2143690