Another answer
您之前的问题解决了这个(引用)问题:
$ cat ipv4.awk
BEGIN { sq = "\x27" } # define single quote
/^ipv4[.](gateway|route)/ { # lines that start with ipv4.gateway or ipv4.route
gsub(/[.-]/,"_",$1) # 1st field: replace period/hyphens with underscore
sub(/:[[:space:]]+/,"=" sq) # replace colon + white space with equal sign and single quote
sub(/$/,sq) # append single quote on end of line
print # print current line
$ awk -f ipv4.awk ipv4settings.txt
ipv4_gateway='192.168.2.1'
ipv4_routes='--'
ipv4_route_metric='-1'
ipv4_route_table='0 (unspec)'
要将这些变量加载到当前环境中,您可以重定向
awk
结果到
source
例如:
$ source <(awk -f ipv4.awk ipv4settings.txt)
$ typeset -p ipv4_gateway ipv4_routes ipv4_route_metric ipv4_route_table
declare -- ipv4_gateway="192.168.2.1"
declare -- ipv4_routes="--"
declare -- ipv4_route_metric="-1"
declare -- ipv4_route_table="0 (unspec)"