On Wed, 6 Sept 2023 at 18:59, Roberto A. Foglietta
<[email protected]> wrote:

> comw_pid()
> {
>     local str
> #   RAF: the /proc/$pid/comm contains the first 15 chars + ending \n
>     str=$(echo -n $*| tr ' ' '\n'| sed -e "s,\(.\{15\}\).*,^\\1$,"| tr '\n' 
> '|')
>     str=$(grep -nE "$str" /proc/[1-9]*/comm 2>/dev/null | cut -d/ -f3)
>     echo $str | grep --color=never .
> }

For those who cares, this is the new version of the function with the
regexp correction:

comw_pid()
{
    local str
#   RAF: the /proc/$pid/comm contains the first 15 chars + an ending \n
    str=$(echo -n $*|tr ' ' '\n'|sed -e 's/\(.\{1,15\}\).*/^\1$/'| tr '\n' '|')
    str=$(grnc -nE "$str" /proc/[1-9]*/comm 2>/dev/null | cut -d/ -f3)
    echo $str | grnc .
}

It can be a replace of pgrep/pidof and can use regular expression like these:

redfishos:~ # comw_pid yamui
623
redfishos:~ # comw_pid 'yamui.*'
619 623 703
redfishos:~ # comw_pid '.*sh'
13900 21934 21935

It is much more faster than using busybox pidof and here there is a
practical example:

redfishos:~ # comw_pid '[a-z]*sh' 'yamui.*' | time cat
13900 23006 23014 23015 619 623 703
real    0m 0.13s

redfishos:~ # time pidof bash ash sh zsh csh dash yamui
13900 13899 11319 722 721 658 1 623
real    0m 0.62s

Searching for every shell plus a process of your own, can be
considered something real and not a corner case like querying 100
process names.

Best regards, R-
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to