One-liner to retrieve path from netstat port

3259 views bash
3

I'm looking to create a one liner that, given a port number (2550) uses the returned value from netstat would allow me to then run the resulting output against ps -ef to return the path of the process in question. I have:

ps -ef | grep $(netstat -tonp | grep 2550 | awk '{split($7,a,"/"); print a[1]}')

and whilst I know

netstat -tonp | grep 2550 | awk '{split($7,a,"/"); print a[1]}'

returns the expected resulted, the subsequent grep tells me that there is no such file or directory (but, if I do the ps -ef | grep **) it works just fine... I'm obviously missing something... well, obvious, but I can't see what?

answered question

1 Answer

8

You just need to quote the output of netstat since there might be multiple PIDs returned by netstat:

ps -ef | grep "$(netstat -tonp | grep 2550 | awk '{split($7,a,\"/\"); print a[1]}')"

posted this

Have an answer?

JD

Please login first before posting an answer.