你可以用Here文档中嵌入AWK在bash脚本? [英] Can you use heredocuments to embed AWK in a bash script?
问题描述
编写shell脚本元左右awk脚本p $ ptty多套。我想有我的语法颜色在awk的部分,因此,我觉得必须有一个更好的方式嵌入awk脚本来砸不仅仅是引用脚本
Writing a shell script that pretty much wraps around an Awk script. I'd like to have my syntax colouring on in the awk section so I feel there must be a better way to embed awk scripts to bash than just quote the script in.
或者是开发一个单独的文件,awk的,并在bash脚本使用的awk -f
直到脚本的完成最好的方式?我想保持在一个文件中所有时间!
Or is it the best way to develop the awk on a separate file and use awk -f
in the bash script until the script's done? I'd like to keep within one file all times!
#!/bin/bash
awkscript='
BEGIN{
print "test"
}
{print $3}'
df | awk "$awkscript"
这是报价的方式,如果你问我,相当难看。任何机会在这里here文档?
This is the quote way, and quite ugly if you ask me. Any chance for heredocs here?
推荐答案
是的,在这猛砸是可能的略少丑陋的方式:
Yes, in Bash this is possible in a slightly less ugly way:
#!/bin/bash
df | awk -f <(cat - <<-'EOD'
BEGIN{
print "test"
}
{print $3}
EOD
)
的≤()
code块被替换为路径(类似于的/ dev / FD / 63
),它指向一个文件描述符绑在code的输出。确保awk脚本缩进制表符,而不是空格(&LT;&LT; -
带前导制表符)。
The <()
code block is replaced with a path (something like /dev/fd/63
) which points to a file descriptor tied to the output of the code. Make sure the awk script is indented with tabs and not spaces (<<-
strips leading tabs).
另一种方式做,这是隐藏在这样的bash脚本的末尾awk脚本(不要忘了退出
)
Another way to do this is to hide the awk script at the end of the bash script like this (don't forget to exit
):
#!/bin/bash
df | awk -f <(sed -e '0,/^#!.*awk/d' $0)
exit $PIPESTATUS
#!/usr/bin/awk -f
BEGIN {
print "test"
}
{print $3}
这篇关于你可以用Here文档中嵌入AWK在bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!