Thursday, April 19, 2012

bash script getting the value from a variable of the form var$[id]

This is the first time Im using a shell script ( #!/bin/sh ) and Ive been working my way through it reading tutorials and the like but Im stuck on this reading and writing values of a key..



Im trying to read in key=value pairs from a config file of the form



key1_begin=abc
key1_end=def

key2_begin=123
key2_end=jkl

.. and so on


I would like the user to pass in parameters to the script like



something.sh 1 x y z


where the first parameter would serve as an that is used to modify the appropriate keys. So after I have checked that the directory exists and the file exists I source it using



source config.cfg


I then save the id using ID=$1 and access the keys using



echo key${ID}_begin 


so a read to obtain the value of the key would be



echo key${ID}_begin = $[key${ID}_begin]


where I expect to get " key1_begin = abc " but instead keep getting " key1_begin = 0 ". The same command however seems to work work for numbers. For example using this command with an ID of 2 gives " key2_begin = 123 "



Could someone please point me in the right direction as to why this works fine for numbers but not alphabets?



And what do I use to change the value of the variable? I am currently using "eval" but this again seems to only work with numbers



[ ! -z $2 ] && eval key${ID}_end=$3


Would really appreciate any advice / pointers with this.\



Thank you





No comments:

Post a Comment