Assigning variable values on the fly in bash

前端 未结 1 1870
鱼传尺愫
鱼传尺愫 2021-01-23 23:44

I have the below config file : OO_CONF.conf with the below contents:

appPackage_name = sqlncli
appPackage_version = 11.3.6538.0

Now i want to r

相关标签:
1条回答
  • 2021-01-24 00:13

    Use the declare built-in in bash with -g flag to make it globally available,

    #!/bin/bash
    
    while IFS=' = ' read -r  key value; do
        declare -g $key="$value"
    done <OO_CONF.conf
    
    printf "%s %s\n" "${appPackage_name}" "${appPackage_version}"
    

    The idea is to set the IFS to = so to split the lines in the file to read the key and value pairs and then with declare we can create variables on the fly with the below syntax

    declare -g <var-name>=<variable-value>
    
    0 讨论(0)
提交回复
热议问题