#!/bin/bash # countprocs.sh - counts each users' processes matching a given regex # Licensed under the terms of the GNU GPLv3 or later # Proprietary Software Must Perish! # AUTHOR: Johannes Truschnigg ( johannes.truschnigg@gmx.at || http://johannes.truschnigg.info ) # some sanity checks; some implementations of `ps` still might fail at a later point (which pgrep && which ps && which printf || echo 1>&2 "please install these missing programs.") >/dev/null || exit 1; [ "${BASH_VERSINFO[0]:-0}" -lt 3 ] && echo 1>&2 "you need bash version 3 or later to run this script." && exit 1; declare -a pid_array; declare -i pid_owner pid_key; # process substitution used to avoid messing with a pipe while read pid_owner; do pid_array[pid_owner]=$((${pid_array[pid_owner]} + 1)); done < <(for i in $(pgrep "${1:-.}"); do ps --no-headers -p "$i" -o "uid"; done) # ${!array[*]} returns keys of an array as of bash 3.x and above for pid_key in ${!pid_array[*]}; do printf "UID %8d: %8d procs\n" "$pid_key" "${pid_array[pid_key]}"; done