LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What does -f- option do for awk command...? (https://www.linuxquestions.org/questions/linux-newbie-8/what-does-f-option-do-for-awk-command-4175464783/)

rahulsh 06-05-2013 02:48 AM

What does -f- option do for awk command...?
 
I have a strange command option -f- used in the code. Here is a snippet of the code

/bin/awk -f- -v "quiet=$QUIET" -v "mapper=$MAPPER" -v "mpt=$MOUNTPOINT" \
<(/bin/cat /etc/fstab 2>/dev/null) \
<<AWKEOF
#then there is a begin block, action block and END block.
END block ends with AWKEOF like this
END {
.............some processing
}
AWKEOF

Tried googling it, but didnt get any pointer

I am not able to understand three things in the program 1
1) -f- option of awk
2) what is AWKEOF means
3) Meaning of < and << in awk context

Could anyone shed some light on this

druuna 06-05-2013 03:13 AM

Awk's -f option normally takes a command file (the awk commands, not the input file(s)) as input. You can however tell it to use the terminal as input (the - after the -f).

the <<AWKEOF and AWKEOF are here-document markers (begin and end).

In your case: All in between <<AWKEOF and AWKEOF is given to awk, awk accepts this due to the -f- option.

grail 06-05-2013 03:44 AM

FWIW ... that is a shocking piece of code. It is obviously part of a larger shell script, so my changes would be:

1. Test for the existence of /etc/fstab earlier in the script so redirection of cat errors into /dev/null shouldn't be required

2. Remove the useless use of cat as awk reads files

3. The use of -f- and a here document seems pointless when you can simply enclose the awk part in single quotes and be done with it

David the H. 06-06-2013 01:52 PM

Actually, I'd say that's a fairly clean and clever way to set up a long awk script without having to worry about weird quoting or readability issues. But it's probably not safe to use unless the heredoc delimiter is also quoted, to disable parameter expansions inside it.

I wouldn't use it unless the script were very long though. And in such cases I'd first try to see if I couldn't do the whole thing in awk, or set up that part as a stand-alone awk script and call that, or else just switch to another language like perl.


By the way, please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.


All times are GMT -5. The time now is 08:02 PM.