作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 AWK
我用来更新的脚本 settings.py
.
我在 Docker 安装期间使用,我正在尝试传递 ENV 变量。
要传递的 ENV 变量:
DB_USER,
DB_PASS,
DB_NAME
awk 'function pr(sp, k, v){ # prints key-value pair with 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'
最佳答案
您可以使用 ENVIRON
GNU Awk
中的变量,
来自 GNU Awk man
page ,
ENVIRON
An associative array containing the values of the environment. The array indices are the environment variable names; the elements are the values of the particular environment variables. For example,
ENVIRON["HOME"]
might be"/home/arnold"
. Changing this array does not affect the environment passed on to any programs thatawk
may spawn via redirection or thesystem()
function. (In a future version ofgawk
, it may do so.)
BEGIN
中执行此操作即可子句加载所有变量并稍后使用
awk 'BEGIN {
db_user = ENVIRON["DB_USER"]
db_pass = ENVIRON["DB_PASS"]
db_name = ENVIRON["DB_NAME"]
}'
使用变量
db_user
,
db_pass
和
db_name
在
Awk
的正文中如你所愿。通过在
BEGIN
中导入环境变量,您不必为输入文件中的每一行导入一次。
关于awk - 如何在 awk 中使用环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46171193/
我是一名优秀的程序员,十分优秀!