File: tip.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # tip [seconds...] [options...]
  27 #
  28 #
  29 # Top with Improved Presets runs `top` using better default settings, and
  30 # offers easier-to-use command-line options. Updates happen every 2 seconds
  31 # by default, unless another number is given; these interval numbers can
  32 # also have decimals.
  33 #
  34 # The options are, available both in single and double-dash versions
  35 #
  36 #   -cpu    reverse-sort entries by latest sampled CPU use
  37 #
  38 #   -h      show this help message
  39 #   -help   show this help message
  40 #
  41 #   -id     sort entries by PID
  42 #   -pid    sort entries by PID
  43 #
  44 #   -m      reverse-sort entries by latest sampled RSS/RES (memory) use
  45 #   -mem    reverse-sort entries by latest sampled RSS/RES (memory) use
  46 #   -res    reverse-sort entries by latest sampled RSS/RES (memory) use
  47 #   -rss    reverse-sort entries by latest sampled RSS/RES (memory) use
  48 #
  49 #   -sh     reverse-sort entries by latest sampled shared memory use
  50 #   -shr    reverse-sort entries by latest sampled shared memory use
  51 #
  52 #   -time   reverse-sort entries by running time
  53 #
  54 #   -usr    sort entries by user
  55 #   -user   sort entries by user
  56 #
  57 #   -vir    reverse-sort entries by latest sampled VIRTual memory use
  58 #   -virt   reverse-sort entries by latest sampled VIRTual memory use
  59 
  60 
  61 case "$1" in
  62     -h|--h|-help|--help)
  63         awk '/^# +tip /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  64         exit 0
  65     ;;
  66 esac
  67 
  68 cmd='top'
  69 sort=''
  70 seconds=2
  71 num_regex='^(\.[0-9]+|[1-9]+(\.[0-9]+)?)$'
  72 
  73 for arg in "$@"; do
  74     case "${arg}" in
  75         --) break;;
  76         -cpu|--cpu) sort="-o %CPU";;
  77         -id|--id|-pid|--pid) sort="-o PID";;
  78         -m|--m|-mem|--mem|-res|--res|-rss|--rss) sort="-o RES";;
  79         -sh|--sh|-shr|--shr) sort="-o SHR";;
  80         -t|--t|-time|--time|-time+|--time+) sort="-o TIME+";;
  81         -usr|--usr|-user|--user) sort="-o USER";;
  82         -vir|--vir|-virt|--virt) sort="-o VIRT";;
  83         *)
  84             if echo "${arg}" | grep -E -q "${num_regex}"; then
  85                 seconds="${arg}"
  86             else
  87                 cmd="${cmd} ${arg}"
  88             fi
  89         ;;
  90     esac
  91 done
  92 
  93 # restore previous screen contents when sent an INT signal, which usually
  94 # happens by pressing Ctrl+C
  95 trap 'tput rmcup; exit 130' INT
  96 
  97 tput smcup
  98 ${cmd} -d "${seconds}" ${sort}
  99 code=$?
 100 tput rmcup
 101 exit "${code}"