Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

New users on my systems commonly ask me "what implements your pps process search?"

When the shell itself filters the output of ps, then removing a grep is unnecessary. Note this uses POSIX shell patterns, not regular expressions.

On a truly POSIX shell that does not support "local," remove the keyword, and change the braces to parentheses to force the function into a subshell.

  pps () { local a= b= c= IFS='\0'; ps ax | while read a
    do [ "$b" ] || c=1; for b; do case "$a" in *"$b"*) c=1;;
      esac; done; [ "$c" ] && printf '%s\n' "$a" && c=; done; }

  $ pps systemd
    PID TTY      STAT   TIME COMMAND
      1 ?        Ss     5:11 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
    557 ?        Ss     0:19 /usr/lib/systemd/systemd-journald
  ...


IFS='\0' doesn't do what it seems: https://github.com/koalaman/shellcheck/wiki/SC2141

You almost always want "read -r": https://github.com/koalaman/shellcheck/wiki/SC2162


Thanks for the critique.

  pps () { local a= b= c= IFS=$'\0'; ps ax | while read -r a
    do [ "$b" ] || c=1; for b; do case "$a" in *"$b"*) c=1;;
      esac; done; [ "$c" ] && printf '%s\n' "$a" && c=; done; }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: