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.
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"
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:
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).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):