我写了这个测试脚本:

#!/bin/bash

build_message='build'

# On first run, the supplied settings block is appended to the supplied config
# file surrounded by comments ("# build START" and "# build END").
# On subsequent runs, the lines in between the two comments will be replaced
# by the provided settings block.
config-insert () {
    settings="$1"
    file="$2"
    awk='BEGIN { p = 1; o = 1; }
        $0 ~ "^# " m " START" { p = 0; if (o) output(); o = 0; }
        $0 ~ "^# " m " END"{ p = 1; next }
        END { if (o) output(o); }
        { if (p) print $0; }
        function output() { print "# " m " START\n" s "\n# " m " END"; }'
    awk -v m="$build_message" -v s="$settings" $awk $file > $file
}

config-insert "setting block" testfile

当我运行它时,我得到一个奇怪的错误:
awk:命令。行:1:开始块必须有一个动作部分

最佳答案

引用$awk:

awk -v m="$build_message" -v s="$settings" "$awk" "$file"

关于linux - awk throw 错误“wk:cmd。行:1:BEGIN块必须具有 Action 部分”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23186261/

10-11 21:59
查看更多