Bash Scripting – Using Awk with Column Numbers

awkbashshell-scripting

I'm using a command like this to get some connections from nf_conntrack:

awk '($3 == "tcp") && ($6 != "TIME_WAIT") && ($10 == "dport=1234")' /proc/net/nf_conntrack

How to use this command inside of a bash script and also add a script argument to it?
If I just do:

awk '($3 == "tcp") && ($6 != "TIME_WAIT") && ($10 == "dport=$1")' /proc/net/nf_conntrack

It will suppose all $1,$3,$6,… are script arguments and fail.
How do I separate awk column numbers from the script arguments to use this command properly?

Best Answer

It is not necessary, but it is the rule of thumb to explicitly declare variables you want to pass from your env to awk

awk -v myvar="dport=$1" '($3 == "tcp") && ($6 != "TIME_WAIT") && ($10 == myvar)' /proc/net/nf_conntrack