[tpwd] Fix segfault when exactly one argument given
[tinyapps.git] / cpuload.sh
blobbc7bb0d795095aa961361f25d7bc6cbb89ed8b83
1 #!/bin/sh
2 ##
3 ## Displays the CPU load.
4 ## By Michal Nazareicz (mina86/AT/mina86.com)
5 ## Released to Public Domain
6 ##
7 ## This is part of Tiny Applications Collection
8 ## -> http://tinyapps.sourceforge.net/
9 ##
11 if [ "X$1" = X-h ] || [ "X$1" = "X--help" ]; then
12 cat <<EOF
13 usage: ${0##*/} [ -n | --nice ]
14 -n --nice includes nice value
15 EOF
19 if [ "X$1" = X-n ] || [ "X$1" = "X--nice" ]; then
20 nice=true
21 else
22 nice=false
26 while true; do
27 head -n 1 /proc/stat
28 sleep 1 || exit
29 done | while true; do
30 read _ a b c d _
31 if $nice; then
32 load=$(( a + b + c ))
33 total=$(( load + d ))
34 else
35 load=$(( a + c ))
36 total=$(( load + b + d ))
39 if [ -z "$ototal" ] || [ x"$total" = x"$ototal" ]; then
40 cpuload=0
41 else
42 cpuload=$((10000 * (load-oload) / (total-ototal)))
45 printf " %3d.%02d%%\r" $((cpuload / 100)) $((cpuload % 100))
47 ototal=$total
48 oload=$load
49 sleep 1
50 done