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 ImProved runs `top` using better default settings, offering
  30 # easier-to-use command-line options. Updates happen every 2 seconds
  31 # by default, unless another number is given; these interval numbers
  32 # can also have decimals.
  33 #
  34 # The options are, available both in single and double-dash versions
  35 #
  36 #   -c      reverse-sort entries by latest sampled CPU use
  37 #   -cpu    reverse-sort entries by latest sampled CPU use
  38 #
  39 #   -h      show this help message
  40 #   -help   show this help message
  41 #
  42 #   -i      sort entries by PID
  43 #   -id     sort entries by PID
  44 #   -pid    sort entries by PID
  45 #
  46 #   -m      reverse-sort entries by latest sampled RSS/RES (memory) use
  47 #   -mem    reverse-sort entries by latest sampled RSS/RES (memory) use
  48 #   -res    reverse-sort entries by latest sampled RSS/RES (memory) use
  49 #   -rss    reverse-sort entries by latest sampled RSS/RES (memory) use
  50 #
  51 #   -shr    reverse-sort entries by latest sampled shared memory use
  52 #
  53 #   -t      reverse-sort entries by running time
  54 #   -time   reverse-sort entries by running time
  55 #
  56 #   -u      sort entries by user
  57 #   -user   sort entries by user
  58 #
  59 #   -v      reverse-sort entries by latest sampled VIRTual memory use
  60 #   -virt   reverse-sort entries by latest sampled VIRTual memory use
  61 
  62 
  63 case "$1" in
  64     -h|--h|-help|--help)
  65         awk '/^# +tip /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  66         exit 0
  67     ;;
  68 esac
  69 
  70 sort=""
  71 seconds=2
  72 num_regex='^(\.[0-9]+|[1-9]+(\.[0-9]+)?)$'
  73 
  74 for arg in "$@"; do
  75     case "${arg}" in
  76         --) break;;
  77         -c|--c|-cpu|--cpu) sort="-o %CPU";;
  78         -i|--i|-id|--id|-pid|--pid) sort="-o PID";;
  79         -m|--m|-mem|--mem|-res|--res|-rss|--rss) sort="-o RES";;
  80         -shr|--shr) sort="-o SHR";;
  81         -t|--t|-time|--time) sort="-o TIME+";;
  82         -u|--u|-user|--user) sort="-o USER";;
  83         -v|--v|-virt|--virt) sort="-o VIRT";;
  84         *)
  85             if echo "${arg}" | grep -E -q "${num_regex}"; then
  86                 seconds="${arg}"
  87             fi
  88         ;;
  89     esac
  90 done
  91 
  92 # restore previous screen contents when sent an INT signal, which usually
  93 # happens by pressing Ctrl+C
  94 trap 'tput rmcup; exit 130' INT
  95 
  96 tput smcup
  97 top -d "${seconds}" ${sort}
  98 code=$?
  99 tput rmcup
 100 exit "${code}"