gpt4 book ai didi

linux - awk 抛出错误 "wk: cmd. line:1: BEGIN blocks must have an action part"

转载 作者:太空宇宙 更新时间:2023-11-04 09:00:04 26 4
gpt4 key购买 nike

我写了这个测试脚本:

#!/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: cmd. line:1: BEGIN blocks must have an action part

最佳答案

将 $awk 放在引号中:

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

关于linux - awk 抛出错误 "wk: cmd. line:1: BEGIN blocks must have an action part",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23186261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com