We are using fuse to find the logs associated to particular port.

fuser /test/log*

This results in a list of logs associated with port

How to find the number of lines on the output?

I tried with |wc - l no luck.

1 Answer 1

$ fuser /usr/bin/zsh
/usr/bin/zsh:         4771e  4778e  7305e  9848e 12416e
$ fuser /usr/bin/zsh > /dev/null
/usr/bin/zsh:       eeeee
$ fuser /usr/bin/zsh 2> /dev/null
  4771  4778  7305  9848 12416

As you can see fuser splits its output between stdout and stderr, where stdout has the pids and stderr has decoration (and errors if any), so to find the number of pids, you can discard stderr and count the number of words in stdout with:

n=$(fuser /test/log* 2> /dev/null | wc -w)

To find the number of unique pids (in case some processes have more than one of the /test/log* files open), in zsh, you could do:

n=${#${(u)$(fuser /test/log/* 2> /dev/null)}}

(which also has the benefit of preserving the exit status of fuser).

Or using lsof instead of fuser:

n=$(lsof -t /test/log/* | wc -l)

Where with -t, lsof gives you the list of pids one per line without duplication.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .