S
S
Sergey2015-12-04 15:08:05
linux
Sergey, 2015-12-04 15:08:05

How to take parameter values ​​from the config file into variables in linux using a bash script?

How to take parameter values ​​from the config file into variables in linux using a bash script?
In general, there is a config, the structure is as follows:


KVM_VM_DISK_2="/var/lib/kvm/storage1/insta1"
KVM_VM_DISK_SIZE_2="2000"

let the config be, for example, in /etc/disk2
How to take the value of the first parameter to the disk variable in bash, and the value of the second parameter to the size variable?
After all, there must be some simpler way to do this than parsing strings? After all, all configs consist of something like this. Or do I just have to mess around with regular expressions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexxandr, 2015-12-04
@butteff

source /etc/disk2

A
abcd0x00, 2015-12-05
@abcd0x00

You just need to write a function that takes the number of the entry in the input stream from which to take the value.
Then you just do

text=`cat file.conf`
val1=`echo "$text" | select_value 1`
val2=`echo "$text" | select_value 2`

And what you are advised by source is wrong: firstly, existing variables will overlap, and secondly, there may be some code that is not like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question