Updated on 5-9-2019 to include the complete and working solution pasted at the very bottom.
Original Problem: First of all, I've searched everywhere for a solution on this site among other sites and no luck.
I'm trying to use the below CASE statement to properly evaluate my custom variable which is concatenated from a string and a count variable together (ex. var_name="cell${i}"). When I echo the custom variable name and value, they appear to be correct.
The problem is that the CASE statement is not evaluating the custom variable correctly (refer to output) and keeps falling into the default condition since no match is found. I need help in figuring out how to have my CASE condition evaluated correctly? Below is the simple code and below the actual code are the different failed variable initialization and case statements including the errors that I get.
#!/bin/sh
cell1="one"
cell2="two"
cell3="three"
i=1
while [ $i -lt 4 ]
do
var_name="cell${i}"
echo "Variable name: ${var_name}"
eval "echo Variable value: \$${var_name}"
case \$${var_name} in #Falls to default case option because variable not evaluated correctly
'one'|'two')
echo "Either one or two: " \$${var_name}
;;
'three')
echo "Only three: " \$${var_name}
;;
*)
echo "Failed"
;;
esac
i=`expr $i + 1`
echo "--------------------------"
done
#Failed variable initialization attempts
#typeset -n var_name=cell${i} #typeset: not found
#echo "${!var_name}" #bad substitution
#Failed case usage attempts
#case ${!var_name} in #bad substitution
#case `eval echo "\$${var_name}"` in #Falls to default case option because variable not evaluated correctly
#case ${var_name} in #Falls to default case option because variable not evaluated correctly
I've tried using typeset -n for the variable and also eval and different placements of "\" and "$" to get this case condition to work properly but no luck. Below is the current output with my comments next to them explaining what I'm expecting to see.
OUTPUT from shell script
--------------------------
Variable name: cell1
Variable value: one
Failed <-- Not being evaluated correctly since it is checking value of "cell1" instead of value "one" so fell into default case option
--------------------------
Variable name: cell2
Variable value: two
Failed <-- Not being evaluated correctly since it is checking value of "cell2" instead of value "two" so fell into default case option
--------------------------
Variable name: cell3
Variable value: three
Failed <-- Not being evaluated correctly for same reasons as above
--------------------------
Working Solution:
#!/usr/bin/ksh
cell1="one"
cell2="two"
cell3="three"
i=1
while [ $i -lt 4 ]
do
var_name="cell"$i
#v=`eval echo ${!var_name}` #solution for bash
v=`eval echo '$'${var_name}` #solution for ksh
echo Variable name: \$${var_name}
echo "Variable value: ${v}"
case ${v} in
'one'|'two')
echo "Either one or two: " \$${var_name}
;;
'three')
echo "Only three: " \$${var_name}
;;
*)
echo "Failed"
;;
esac
i=`expr $i + 1`
echo "--------------------------"
done
Working Code Output
Variable name: $cell1
Variable value: one
Either one or two: $cell1
--------------------------
Variable name: $cell2
Variable value: two
Either one or two: $cell2
--------------------------
Variable name: $cell3
Variable value: three
Only three: $cell3
--------------------------