0

Try to extract the process ID that is using a given port number from the fuser output as below

$ fuser 9092/tcp
9092/tcp:             5920

Extracting the PID using awk is not happening

$ fuser 9092/tcp | awk -F: '{print $2}'
9092/tcp:

from the extracted PID, I want to do ls -l /proc/5920/exe like

ls -l /proc/$(fuser 9092/tcp | awk -F: '{print $2}')/exe

Versions of the binary as below:

bash --version  # GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
fuser --version  # fuser (PSmisc) 22.20
Ibrahim Quraish
  • 3,889
  • 2
  • 31
  • 39

1 Answers1

1

The informal part of the output from fuser goes to standard error. The output to standard output is already computer-readable. (This is a feature, not a bug.)

Trivially, you can redirect standard error to get rid of the stderr output if you think it's useless or distracting.

$ fuser 9092/tcp 2>/dev/null
5920

You can easily establish this for yourself by piping e.g. to nl

$ fuser 9092/tcp | nl
9092/tcp: 
1       5920

Notice how the 9092/tcp: output does not get a line number -- it's not being piped to nl.

So your actual code could look like

ls -l /proc/$(fuser 9092/tcp 2>/dev/null)/exe

(though usually don't use ls in scripts.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • It works this way for me with the help of your solution `ls -l /proc/$(fuser 9092/tcp 2>/dev/null | awk -F'[' '{print $1}' | sed 's/ //g')/exe` or `ls -l /proc/$(fuser 9092/tcp 2>/dev/null | nl | awk '{print $2}')/exe` – Ibrahim Quraish Sep 29 '20 at 10:27
  • The Awk and `sed` junk is completely unnecessary. The `nl` is just to help you see what's going on, and not part of the solution. – tripleee Sep 29 '20 at 10:28