代码之家  ›  专栏  ›  技术社区  ›  aenagy

Bash声明不喜欢带空格的值

  •  0
  • aenagy  · 技术社区  · 5 月前

    这是一个延续 awk gsub not replacing all instances of period in field .

    我有一些意见:

    $ cat ./ipv4settings.txt
    ipv4.gateway:                           192.168.2.1
    ipv4.routes:                            --
    ipv4.route-metric:                      -1
    ipv4.route-table:                       0 (unspec)
    

    我能够(在一定程度上)生成所需的输出:

    $ awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"="$2 }' ./ipv4settings.txt
    ipv4_gateway=192.168.2.1
    ipv4_routes=--
    ipv4_route_metric=-1
    ipv4_route_table=0 (unspec)
    

    接下来我要做的是为每一行输出声明变量。我使用了几个变体 declare :

    $awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"="$2 }' ./ipv4settings.txt
    ipv4_gateway=192.168.2.1
    ipv4_routes=--
    ipv4_route_metric=-1
    ipv4_route_table=0 (unspec)
    $ declare $( awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"="$2 }' ./ipv4settings.txt )
    -bash: declare: `(unspec)': not a valid identifier
    

    我试着引用了 awk ( declare not a valid identifier bash )( https://www.baeldung.com/linux/awk-print-quote-characters ):

    $ awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print "\042"$1"="$2"\042" }' ./ipv4settings.txt
    "ipv4_gateway=192.168.2.1"
    "ipv4_routes=--"
    "ipv4_route_metric=-1"
    "ipv4_route_table=0 (unspec)"
    $ declare $( awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print "\042"$1"="$2"\042" }' ./ipv4settings.txt )
    -bash: declare: `"ipv4_gateway=192.168.2.1"': not a valid identifier
    -bash: declare: `"ipv4_routes=--"': not a valid identifier
    -bash: declare: `"ipv4_route_metric=-1"': not a valid identifier
    -bash: declare: `"ipv4_route_table=0': not a valid identifier
    -bash: declare: `(unspec)"': not a valid identifier
    
    

    …或者只是价值部分:

    $ awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"=\042"$2"\042" }' ./ipv4settings.txt
    ipv4_gateway="192.168.2.1"
    ipv4_routes="--"
    ipv4_route_metric="-1"
    ipv4_route_table="0 (unspec)"
    $ declare $( awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"=\042"$2"\042" }' ./ipv4settings.txt )
    -bash: declare: `(unspec)"': not a valid identifier
    

    我怎么得到 声明 使用带空格的变量值?

    2 回复  |  直到 5 月前
        1
  •  2
  •   markp-fuso    5 月前

    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)"
    
        2
  •  0
  •   ticktalk    5 月前

    下面所示的内容是否合适?

    $ cat ipv4settings.txt
    ipv4.gateway:                           192.168.2.1
    ipv4.routes:                            --
    ipv4.route-metric:                      -1
    ipv4.route-table:                        0 (unspec)
    
    $ cat fixme.awk
    {printf("%s=\"%s\"\n",gensub(/[\.-]/,"_","g",$1),gensub(/ {2,}/,"","g",$2))}
    
    $ awk -F: -f fixme.awk ipv4settings.txt
    ipv4_gateway="192.168.2.1"
    ipv4_routes="--"
    ipv4_route_metric="-1"
    ipv4_route_table="0 (unspec)"