# variable inside variable inside loop headache

**URL:** <https://community.unix.com/t/variable-inside-variable-inside-loop-headache/242788>\
**Category:** Shell Programming and Scripting\
**Created:** [June 18, 2009, 10:29am UTC](https://community.unix.com/t/variable-inside-variable-inside-loop-headache/242788 "2009-06-18T10:29:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hcclnoodles](https://community.unix.com/letter_avatar/hcclnoodles/32/5_5575768a8748004e209b776fc1b2916d.png) [@hcclnoodles](https://community.unix.com/u/hcclnoodles)\
**Post date:** [June 18, 2009, 10:29am UTC](https://community.unix.com/t/variable-inside-variable-inside-loop-headache/242788/1 "2009-06-18T10:29:20Z")

</div>

Hi Gurus

I have a file called /tmp/CMDB which looks like this

```nohighlight
serial: 0623AN1208
hostname: server1
model: x4100
assetID: 1234

```

I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as an example, I want it to create a variable called

db\_serial and populate it with value 0623AN1208

on the next iteration create a variable called

db\_hostname and populate it with value server1

etc etc

so i have tried

```nohighlight
cat /tmp/CMDB | while read n
do

VAR=db_`echo $n | cut -d: -f1`
VALUE=`echo $n | cut -d: -f2`

eval `echo $VAR`=`echo $VALUE`

done

# echo it all out after the loop has finished

echo "DB serial is set to $db_serial"
echo "DB hostname is set to $db_hostname"
echo "DB model is set to $db_model"
echo "DB Asset ID is set to $db_assetID"

```

Plus a whole load of other combinations and i just cant get the thing to work

Im sure the problem is with my use of the "eval" command which is effectively using another variable to create a variable, and assigning the value from another variable :eek:

does anybody know what im doing wrong ?😕

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [June 18, 2009, 10:43am UTC](https://community.unix.com/t/variable-inside-variable-inside-loop-headache/242788/2 "2009-06-18T10:43:40Z")

</div>

How about this --

```nohighlight
while read col1 col2
do
   case "$col1" in
		"serial:") db_serial=$col2;;
      'hostname:') db_hostname=$col2;;      
         "model:") db_model=$col2;;
       "assetID:") db_asssetID=$col2;;
       *);;
   esac                         
   echo "db_serial= $db_serial "
   echo "db_hostname= $db_hostname "
   echo "db_model= $db_model "
   echo "db_asssetID= $db_asssetID "
    
done < filename

```

---

<div class="post-metadata">

**Author:** ![hcclnoodles](https://community.unix.com/letter_avatar/hcclnoodles/32/5_5575768a8748004e209b776fc1b2916d.png) [@hcclnoodles](https://community.unix.com/u/hcclnoodles)\
**Post date:** [June 18, 2009, 10:55am UTC](https://community.unix.com/t/variable-inside-variable-inside-loop-headache/242788/3 "2009-06-18T10:55:03Z")

</div>

thanks Jim, the thing is, the file /tmp/CMDB will have thousands of values. i have just given four (serial, hostname, assetid and model). The object of the script is to create a variable from the name defined to the left of the colon regardless of what it is, so my script shouldn't have any references to the "actual" variable names. e.g hostname or assetID etc.

If that file happens to have a line like this

```nohighlight
bananas: woohoo

```

then i want a variable created called db\_bananas with a value of "woohoo" ... but it will always be the case that ill have no idea what will be in that file

---

<div class="post-metadata">

**Author:** ![tkleczek](https://community.unix.com/letter_avatar/tkleczek/32/5_5575768a8748004e209b776fc1b2916d.png) [@tkleczek](https://community.unix.com/u/tkleczek)\
**Post date:** [June 18, 2009, 12:08pm UTC](https://community.unix.com/t/variable-inside-variable-inside-loop-headache/242788/4 "2009-06-18T12:08:25Z")

</div>

The problem is not the eval expression, but the fact that pipes are executed in subshells. Therefore, variables created in while loop remain unset outside of it.  
Try redirecting input without using a pipe:

```nohighlight
while read n
do

VAR=db_`echo $n | cut -d: -f1`
VALUE=`echo $n | cut -d: -f2`

eval `echo $VAR`=`echo $VALUE`

done < /tmp/CMDB

# echo it all out after the loop has finished

echo "DB serial is set to $db_serial"
echo "DB hostname is set to $db_hostname"
echo "DB model is set to $db_model"
echo "DB Asset ID is set to $db_assetID"

```

Also, the $VALUE variable has a leading space in it. You should strip it off, by using

```nohighlight
VALUE="${VALUE:1}"

```

---

<div class="post-metadata">

**Author:** ![danmero](https://community.unix.com/letter_avatar/danmero/32/5_5575768a8748004e209b776fc1b2916d.png) [@danmero](https://community.unix.com/u/danmero)\
**Post date:** [June 18, 2009, 12:33pm UTC](https://community.unix.com/t/variable-inside-variable-inside-loop-headache/242788/5 "2009-06-18T12:33:38Z")

</div>

> [@hcclnoodles](#):
>
> The object of the script is to create a variable from the name defined to the left of the colon regardless of what it is, so my script shouldn't have any references to the "actual" variable names. e.g hostname or assetID etc.

This should work for you.

```plaintext
awk -F:\ 'NF{printf "db_%s=%s\n",$1,$2}' file

```

Next time try to solve your own problems by yourself:cool:

---

<div class="post-metadata">

**Author:** ![rubin](https://community.unix.com/user_avatar/community.unix.com/rubin/32/743_2.png) [@rubin](https://community.unix.com/u/rubin)\
**Post date:** [June 18, 2009, 1:07pm UTC](https://community.unix.com/t/variable-inside-variable-inside-loop-headache/242788/6 "2009-06-18T13:07:15Z")

</div>

```nohighlight
eval `awk -F':| +' '{ print "db_" $1 "=" $NF }' file`

```

- Assuming the given file format, and db\_ is the desired prefix for all records.

---

<div class="post-metadata">

**Author:** ![hcclnoodles](https://community.unix.com/letter_avatar/hcclnoodles/32/5_5575768a8748004e209b776fc1b2916d.png) [@hcclnoodles](https://community.unix.com/u/hcclnoodles)\
**Post date:** [June 18, 2009, 4:55pm UTC](https://community.unix.com/t/variable-inside-variable-inside-loop-headache/242788/7 "2009-06-18T16:55:43Z")

</div>

> [@danmero](#):
>
> This should work for you.
> 
> ```plaintext
> awk -F:\ 'NF{printf "db_%s=%s\n",$1,$2}' file
> 
> ```
> 
> Next time try to solve your own problems by yourself:cool:

Thank you danmero and others, With respect to your comment above, you will see from the OP that i have indeed given clear examples of what I had tried and I have expressed that after multiple attempts at different solutions i found myself banging my head against a wall with this one.... So to say " try to solve your own problems !" I think is a little harsh

Thank you for your help though 🙂
