shell script Variable

hi all,

i want use the variable value as a new variable name. And i want to use this new variable value

for i in COMPUTER1 COMPUTER2
do
flag_name=${i}_FLAG
eval ${flag_name}=123
echo $i'_FLAG'
done

output is
COMPUTER1_FLAG
COMPUTER2_FLAG

i need output as 123

You need an eval around the echo too. And a literal dollar sign in front of the variable.

eval echo \$${i}_FLAG

It will print 123 twice, of course, when the loop runs twice.

Its Working. Thank U :slight_smile:

hi era,

your given code (eval echo \$${i}_FLAG) is woking fine.

now i want to use the variable value to in if condiation..
is following code is right. please help

for i in COMPUTER1 COMPUTER2
do
flag_name=${i}_FLAG
eval ${flag_name}=1
if [ `eval echo \$${i}_FLAG` -eq 1 ]
then
echo yes
else
echo no
fi
done

With the backticks, you will be requiring even more backslashes. But I'd recommend against using backticks, and "if test -eq".

eval 'case $'${i}'_FLAG in 1) echo yes;; *) echo no;; esac'