variable inside variable inside loop headache


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable inside variable inside loop headache
# 1  
variable inside variable inside loop headache

Hi Gurus

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

Code:
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

Code:
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 Smilie

does anybody know what im doing wrong ?Smilie
# 2  
How about this --
Code:
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

# 3  
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

Code:
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
# 4  
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:
Code:
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
Code:
VALUE="${VALUE:1}"

# 5  
Quote:
Originally Posted by 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.
Code:
awk -F:\  'NF{printf "db_%s=%s\n",$1,$2}' file

Next time try to solve your own problems by yourselfSmilie

Last edited by danmero; 06-18-2009 at 05:34 PM.. Reason: Change code
# 6  
Code:
eval `awk -F':| +'  '{ print "db_" $1 "=" $NF }' file`


- Assuming the given file format, and db_ is the desired prefix for all records.
# 7  
Quote:
Originally Posted by danmero
This should work for you.
Code:
awk -F:\  'NF{printf "db_%s=%s\n",$1,$2}' file

Next time try to solve your own problems by yourselfSmilie

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 Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #132
Difficulty: Easy
Many of the command line and graphical utilities in a Linux distro are very similar to a Unix system.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger. if ] then echo "Only satisfied variable with value" ... (3 Replies)
Discussion started by: prince1987
3 Replies

2. UNIX for Beginners Questions & Answers

Variable inside while loop got reset

hi, I am using hp unix server and not getting variable output present inside the while loop. I have tried changing the code and need to verify whether it is proper practice of code. I am expecting the output of varible RUN_FILE 3 to TRUE which i get inside the while loop. RUN_FILE 1=TRUE... (8 Replies)
Discussion started by: gowthamsoft
8 Replies

3. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

4. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

5. Shell Programming and Scripting

Automatic variable assignment inside a for loop

cs1=`echo "scale=8;($css1/$css0)*100"|bc` cs2=`echo "scale=8;($css2/$css0)*100"|bc` cs3=`echo "scale=8;($css3/$css0)*100"|bc` cs4=`echo "scale=8;($css4/$css0)*100"|bc` cs5=`echo "scale=8;($css5/$css0)*100"|bc` cs6=`echo "scale=8;($css6/$css0)*100"|bc` cs7=`echo "scale=8;($css7/$css0)*100"|bc`... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

6. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

7. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

8. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

9. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies

Featured Tech Videos