How do I use environment variables in Awk -


i've got following awk script i'm using update settings.py. utilising during docker install , i'm trying pass env variables.

env variables pass:

db_user, db_pass, db_name 

the code below

awk 'function pr(sp, k, v){    # prints key-value pair indentation          printf "%s\047%s\047: \047%s\047,\n",sp,k,v;       }      /sqlite/{ sub(/sqlite[0-9]*/,"mysql",$0) }      /name/{ sp=substr($0,1,index($0,"\047")-1);               print sp$1" \047$db_name\047";               pr(sp,"user","$db_user"); pr(sp,"password","$db_pass");               pr(sp,"host","localhost"); pr(sp,"port",""); next       }1' 

you can use environ variable in gnu awk,

from gnu awk man page,

environ

an associative array containing values of environment. array indices environment variable names; elements values of particular environment variables. example, environ["home"] might "/home/arnold". changing array not affect environment passed on programs awk may spawn via redirection or system() function. (in future version of gawk, may so.)

using above array reference environment variables, in begin clause load variables , use later

awk 'begin {     db_user = environ["db_user"]     db_pass = environ["db_pass"]     db_name = environ["db_name"]  }' 

use variables db_user, db_pass , db_name in body of awk wish. importing environment variables in begin, don't have import them once each line in input file.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -