#!/bin/sh

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
 cat <<HELP
Get PIDs
https://benalman.com/

Usage: $(basename "$0") [processname [processname ...]]

Get the PIDs of all matching processes for all specified processnames.
If no arguments are passed, list processes by PID, TTY, USER, COMMAND.

Copyright (c) 2012 "Cowboy" Ben Alman
Licensed under the MIT license.
https://benalman.com/about/license/
HELP
  exit
fi

if [ -z "$1" ]; then
  ps axo pid,tty,user,command
else
  args="$1"
  shift
  for arg in "$@"; do
    args="${args}|${arg}"
  done
  # shellcheck disable=SC2005,SC2250,SC2312
  echo $(ps axo pid,tty,user,command \
    | perl -nle"m#^\s*(\d+).*(?:$args)# && !m#.dotfiles/bin/pid# && print \$1")
fi
