1

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
--------------------------
eg7567
  • 11
  • 3
  • 1
    https://unix.stackexchange.com/questions/41406/use-a-variable-reference-inside-another-variable . For ksh, look at the `typeset -n` answer. – Mark Plotnick May 09 '19 at 01:54
  • Hi "that other guy". I did try your suggestion about "typeset -n" and I also checked the 2 links provided but they didn't work for me. I don't think the problem is in the variable creation/initialization. Instead, I believe the problem is in the CASE condition. I updated my question & explanation to try to make my issue clearer. – eg7567 May 09 '19 at 05:35
  • I'm not "that other guy", but I did suggest typeset -n. What is the output of `echo $KSH_VERSION` – Mark Plotnick May 09 '19 at 05:57
  • Sorry Mark. I'm new to this forum but quickly getting the hang of it. – eg7567 May 09 '19 at 12:58
  • Below is the ksh version as requested. Thanks in advance for responding. #echo $KSH_VERSION ## Returned nothing #strings /bin/ksh | grep Version | tail -2 @(#)Version M-11/16/88i – eg7567 May 09 '19 at 13:04
  • Mark, I figured it out with the help of a few co-workers. I just need to figure out how to provide the solution here in Stack Overflow. I may have to edit my question and apply the complete solution there. Thank you for your help – eg7567 May 09 '19 at 14:30
  • Glad you found a solution for ksh88. I've voted to reopen the question; when enough others have done so, a text area will appear at the bottom of this page and you can put your answer there. – Mark Plotnick May 09 '19 at 14:49

0 Answers0