从bash读取java .properties文件
发布时间:2020-12-15 19:57:52 所属栏目:安全 来源:网络整理
导读:我正在考虑使用sed来阅读.properties文件,但是想知道是否有更聪明的方式从bash脚本中做? 上述解决方案将适用于基础知识。我不认为它们涵盖多行值。这是一个awk程序,将从stdin解析Java属性,并将shell环境变量生成为stdout: BEGIN { FS="="; print "# BEG
我正在考虑使用sed来阅读.properties文件,但是想知道是否有更聪明的方式从bash脚本中做?
上述解决方案将适用于基础知识。我不认为它们涵盖多行值。这是一个awk程序,将从stdin解析Java属性,并将shell环境变量生成为stdout:
BEGIN { FS="="; print "# BEGIN"; n=""; v=""; c=0; # Not a line continuation. } /^#/ { # The line is a comment. Breaks line continuation. c=0; next; } /$/ && (c==0) && (NF>=2) { # Name value pair with a line continuation... e=index($0,"="); n=substr($0,1,e-1); v=substr($0,e+1,length($0) - e - 1); # Trim off the backslash. c=1; # Line continuation mode. next; } /^[^]+$/ && (c==1) { # Line continuation. Accumulate the value. v= "" v substr($0,length($0)-1); next; } ((c==1) || (NF>=2)) && !/^[^]+$/ { # End of line continuation,or a single line name/value pair if (c==0) { # Single line name/value pair e=index($0,"="); n=substr($0,e-1); v=substr($0,length($0) - e); } else { # Line continuation mode - last line of the value. c=0; # Turn off line continuation mode. v= "" v $0; } # Make sure the name is a legal shell variable name gsub(/[^A-Za-z0-9_]/,"_",n); # Remove newlines from the value. gsub(/[nr]/,"",v); print n "="" v """; n = ""; v = ""; } END { print "# END"; } 如您所见,多行值使事情变得更加复杂。要在shell中查看属性的值,只需在输出中输入: cat myproperties.properties | awk -f readproperties.awk > temp.sh source temp.sh 这些变量将在’。’的位置有’_’,所以属性some.property将在shell中为some_property。 如果您有具有属性插值的ANT属性文件(例如’$ {foo.bar}’)),那么我建议使用Groovy与AntBuilder。 这里是my wiki page on this very topic。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |