从bash读取java .properties文件

前端之家收集整理的这篇文章主要介绍了从bash读取java .properties文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在考虑使用sed来阅读.properties文件,但是想知道是否有更聪明的方式从bash脚本中做?
上述解决方案将适用于基础知识。我不认为它们涵盖多行值。这是一个awk程序,将从stdin解析Java属性,并将shell环境变量生成为stdout: @H_403_5@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(/[\n\r]/,"",v); print n "=\"" v "\""; n = ""; v = ""; } END { print "# END"; }

如您所见,多行值使事情变得更加复杂。要在shell中查看属性的值,只需在输出中输入:

@H_403_5@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

原文链接:https://www.f2er.com/bash/388810.html

猜你在找的Bash相关文章