File: tip.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 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 # As when running `top` directly, pressing `q` quits, while pressing `h` shows 35 # the help screen, which you also quit by pressing `q` while in it. You can 36 # also force-quit this tool by pressing Ctrl+C. 37 # 38 # The options are, available both in single and double-dash versions 39 # 40 # -cpu reverse-sort entries by latest sampled CPU use 41 # 42 # -h show this help message 43 # -help show this help message 44 # 45 # -id sort entries by PID 46 # -pid sort entries by PID 47 # 48 # -m reverse-sort entries by latest sampled RSS/RES (memory) use 49 # -mem reverse-sort entries by latest sampled RSS/RES (memory) use 50 # -res reverse-sort entries by latest sampled RSS/RES (memory) use 51 # -rss reverse-sort entries by latest sampled RSS/RES (memory) use 52 # 53 # -sh reverse-sort entries by latest sampled shared memory use 54 # -shr reverse-sort entries by latest sampled shared memory use 55 # 56 # -time reverse-sort entries by running time 57 # 58 # -usr sort entries by user 59 # -user sort entries by user 60 # 61 # -vir reverse-sort entries by latest sampled VIRTual memory use 62 # -virt reverse-sort entries by latest sampled VIRTual memory use 63 64 65 case "$1" in 66 -h|--h|-help|--help) 67 awk '/^# +tip /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 68 exit 0 69 ;; 70 esac 71 72 cmd='top' 73 sort='' 74 seconds=2 75 number_regex='^(\.[0-9]+|[1-9]+(\.[0-9]+)?)$' 76 77 for arg in "$@"; do 78 if [ "${arg}" = '--' ]; then 79 break 80 fi 81 82 case "${arg}" in 83 -cpu|--cpu) sort="-o %CPU";; 84 -id|--id|-pid|--pid) sort="-o PID";; 85 -m|--m|-mem|--mem|-res|--res|-rss|--rss) sort="-o RES";; 86 -sh|--sh|-shr|--shr) sort="-o SHR";; 87 -t|--t|-time|--time|-time+|--time+) sort="-o TIME+";; 88 -usr|--usr|-user|--user) sort="-o USER";; 89 -vir|--vir|-virt|--virt) sort="-o VIRT";; 90 *) 91 if echo "${arg}" | grep -E -q "${number_regex}"; then 92 seconds="${arg}" 93 else 94 cmd="${cmd} ${arg}" 95 fi 96 ;; 97 esac 98 done 99 100 # restore previous screen contents when sent an INT signal, which usually 101 # happens by pressing Ctrl+C 102 trap 'tput rmcup; exit 130' INT 103 104 tput smcup 105 ${cmd} -d "${seconds}" ${sort} 106 code=$? 107 tput rmcup 108 exit "${code}"