0

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?

1 Answer 1

1

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 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.