简体   繁体   中英

Printing environment variable inside bash script

Im trying to write simple shell script to parse a property file and compute another string based on those values.

dev.properties

com.global.jdbcUrl=${local_jdbcUrl}

Environment variable

export local_jdbcUrl="jdbc:mysql://localhost:3306/db_0?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true"

bash file:

#!/bin/bash

file="./dev.properties"

function prop {
  grep "${1}" ${file} | cut -d'=' -f2
}

text="global"
keys="jdbcUrl"

IFS=','
read -a strarr <<< "$text"
read -a keysarr <<< "$keys"

echo "There are ${#strarr[*]} words in the text.\n"
echo "There are ${#keysarr[*]} words in the text. \n\n"

for val in "${strarr[@]}";
do
  for refs in "${keysarr[@]}";
  do
    tmp="$val.$refs"
    printf "name : $tmp\n\n"

    valu="$(prop $tmp)"
    printf "value : $valu \n"

    printenv local_jdbcUrl
  done
done

Output of this script:

There are 1 words in the text.\n
There are 1 words in the text. \n\n
name : global.jdbcUrl

value : ${local_jdbcUrl} 
jdbc:mysql://localhost:3306/db_0?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true

I'm running migrations using shell script so to form a migration string command I need to have the actual environment variable value in a string. The environment variable substitution is not happening. Not sure what is wrong here. Can someone kindly help I'm new to bash script. I checked the environment value in the shell script and it's working fine, only inside the bash script I'm getting this error.

Perhaps envsubst

$ cat file
${HOME} is where the heart is

$ cat file | envsubst
/home/glennj is where the heart is

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM