Reading values ​​from configuration file and using shell script

I have a text configuration file something like this:

## COMMENT KEY1=VALUE1 ## COMMENT KEY2=VALUE2 KEY3=VALUE3 ## COMMENT ## COMMENT 

As you can see, this one has key value pairs, however it also contains comment lines and empty lines. In some cases, comments are on the same line as a pair of key values.

How to read this configuration file and set the keys as variable names in a shell script so that I can use them like:

 echo $KEY1 
+4
source share
3 answers

only

 source config.file 

then you can use these variables in your shell.

+9
source

Just enter the code at the beginning of your code:

 . file 

or

 source file 
+2
source

For example, here is the contents of your configuration file:

 email=test@test.com user=test password=test 

There are two ways:

  • Use the source to do this.

     source $<your_file_path> echo $email 
  • read the contents and then scroll through each line to compare to determine the correct line

     cat $<your_file_path> | while read line do if [[$line == *"email"*]]; then IFS='-' read -a myarray <<< "$line" email=${myarray[1]} echo $email fi done 

The second drawback of the solution is what you need to use to check each line.

+2
source

Source: https://habr.com/ru/post/1502078/


All Articles