#!/bin/bash # # homespace [-px] [user|dir] # # Display the amount of space left in the users home and other partitions # user has quotas for. If no quota is found display the space used/left # on the users home partition. # # -p Check time to password expiry if posible # -x Output as a X window bar graph (using "zenity") # ### # # Source Location: Anthony's Home ~anthony/bin/scripts # Distributed By: Manually to lyrch:/source/opt/bin_scripts # for "Opt Install" into /opt/bin on all machines # # Original idea by some unknown student at Griffith University, 1992 # Completely re-written several times by Anthony Thyssen # # ----- # Discover where the shell script resides PROGNAME=`type "$0" | awk '{print $3}'` # search for executable on path PROGDIR=`dirname "$PROGNAME"` # extract directory of program PROGNAME=`basename "$PROGNAME"` # base name of program Usage() { # output the script comments as docs echo >&2 "$PROGNAME:" "$@" sed >&2 -n '/^###/q; /^#/!q; s/^#//; s/^ //; 3s/^/Usage: /; 2,$ p' \ "$PROGDIR/$PROGNAME" exit 10; } # Peferred default user entry # Specifically I prefer my own login name when I has sudo'ed to root user="$USER" [ ! "$user" ] && user=$LOGNAME [ ! "$user" ] && user=`whoami` home="$HOME" # home to check, (unless a disk quota is found) QUOTA_TIMEOUT=5 while [ $# -gt 0 ]; do case "$1" in --help|--doc*) Usage ;; -p) DO_PWEXP=true ;; -x) FORCE_X=true ;; --) shift; break ;; # end of user options -*) Usage "Unknown option \"$1\"" ;; *) break ;; # end of user options esac shift # next option done case "$1" in "") ;; /*) home="$1" # a specific absolute directory was given ;; *) user="$1" # assume it is a specific user (find there home directory) # try to locate the users home directory - fails invalid UIDs home=$( bash -c "echo ~$user" ) 2>/dev/null home=$( ( cd $home 2>/dev/null && pwd ) || echo $home ) ;; esac # Is the command available? type -t cmd_found >/dev/null || cmd_found() { type -t "$1" >/dev/null; } do_quota() { # lookup the users disk quota but with a timeout #/usr/bin/timeout $QUOTA_TIMEOUT \ timeout $QUOTA_TIMEOUT \ quota -v "$@" 2>&1 | grep -v '^quota: error .* Connection refused' } case `uname -sr` in 'Linux'*) awk=gawk df_opt=-k n2=-n+2 ;; 'SunOS 4'*) awk=nawk df_opt= n2=+2 ;; 'Darwin'*) awk=awk df_opt= n2=+2 ;; # MacOsX *) awk=nawk df_opt=-k n2=+2 ;; esac case `uname -s` in 'IRIX') size_idx=3; used_idx=4; free_idx=5; percent_idx=6; mount_idx=7 ;; *) size_idx=2; used_idx=3; free_idx=4; percent_idx=5; mount_idx=6 ;; esac # ----- Gather info ------------ # first use awk to fix any records split accross multiple lines due to length # then in second awk, # skip a quota if automount is in use and home is not the users home # then print any user quotas with non-zero space. if cmd_found quota && [ -z "$home" ]; then quotas=`do_quota "$user"` quotas=`echo "$quotas" | sed 's/\*//g' | $awk 'NF == 1 { getline l; print $0, l; next; } { print; } ' | $awk 'BEGIN { AUTOHOME="'"$autohome"'"+0 } AUTOHOME && $1 !~ /\/home\/'"$user"'/ { next } NR>2 && $2+0 && $3+0' ` fi if [ "$quotas" ]; then # Sub-shell variable assignment failure!!!! #echo "$quotas" | #while read quota; do set - $quotas mount=`echo $1 | sed 's/\..*:/:/'` used=$2 limit=$3 left=$(( used < limit ? limit - used : 0 )) percent=$(( used * 100 / limit )) # percentage used if [ "$limit" -lt 80000 ]; then used="$used"Kb limit="$limit"Kb left="$left"Kb else used=$(( ( used + 512 ) / 1024 ))Mb # fudged values limit=$(( ( limit + 512 ) / 1024 ))Mb left=$(( ( left + 512 ) / 1024 ))Mb fi title="Quota for $user in $mount ( $used / $limit ) or $left left" #done else # gather info - the xargs echo merges multiple lines of output from df set - $( df $df_opt $home | tail $n2 | xargs echo ) eval size=\$$size_idx eval used=\$$used_idx eval free=\$$free_idx eval 'percent=`echo $'$percent_idx' | sed "s/%//"`' eval mount=\$$mount_idx if [ "$size" -gt 10000000 ]; then used=$(( ( ( used + 512 ) / 1024 + 512 ) / 1024 ))Gb size=$(( ( ( size + 512 ) / 1024 + 512 ) / 1024 ))Gb free=$(( ( ( free + 512 ) / 1024 + 512 ) / 1024 ))Gb else used=$(( ( used + 512 ) / 1024 ))Mb # round into Gbytes size=$(( ( size + 512 ) / 1024 ))Mb free=$(( ( free + 512 ) / 1024 ))Mb fi title="Disk Space in $mount ( $used / $size ) or $free left" fi if [ "$FORCE_X" ] && cmd_found zenity; then if zenity --help-all | grep timeout; then timeout='--timeout 20' # add a twenty second timeout fi >/dev/null 2>&1 zenity --progress --title HomeSpace $timeout \ --percentage=$percent --text="$title" /dev/null fi