Sep 072010
 

Filename: disktest.sh

#!/bin/bash
# This script does a very simple test for checking disk space.

space=`df -h | awk ‘{print $5}’ | grep % | grep -v Use | sort -n | tail -1 | cut -d “%” -f1 -`

case $space in
[1-6]*)
  Message=”All is quiet.”
  ;;
[7-8]*)
  Message=”Start thinking about cleaning out some stuff.  There’s a partition that is $space % full.”
  ;;
9[1-8])
  Message=”Better hurry with that new disk…  One partition is $space % full.”
  ;;
99)
  Message=”I’m drowning here!  There’s a partition at $space %!”
  ;;
*)
  Message=”I seem to be running with an nonexistent amount of disk space…”
  ;;
esac

Sep 072010
 

It’s possible to use even more complicated syntax with regular expressions:

I used this in my FTP script for safety surveillor.

case “$1? in
+(start|run) ) /usr/app/startup-script ;;
@([Ss])top ) /usr/app/stop-script ;;
esac

?(pattern1 | pattern2 | … | patternn)
zero or one occurrence of any pattern

*( pattern1 | pattern2 | … | patternn)
zero or more occurrences of any pattern

@( pattern1 | pattern2 | … | patternn)
exactly one occurrence of any pattern

+( pattern1 | pattern2 | … | patternn)
one or more occurrence of any pattern

!( pattern1 | pattern2 | … | patternn)
all strings except those that match any pattern

Sep 052010
 

The passwords have been removed for obvious reasons.

The fucntion works now! I couldn’t figure out how to force the user to input a number for the second argument but I figured out a work-around to force the user to input two arguments. If they don’t it will present a message telling them what they need to do. I also had to add a bogus option (xyzpdq) in front of the user names in the “case” section. For some reason the script ignores whatever is in the first position.

sendtoftp() {
if [ $# -ne 2 ]
then
echo
echo “————————————–”
echo “| Example: sendtoftp ban11 09122010 |”
echo “————————————–”
echo
else

case $1 in
ban11)
HOST=10.65.48.199
USER=ban11
PASSWD=xxxxxx

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
#print -p cd directory
#print -p binary
print -p prompt
print -p ascii
print -p mput ban11*$2*
print -p bye

wait
exit 0
;;
@xyzpdq | ban02 | ban03 | ban04 | ban05 | ban06 | ban07 | ban08 | ban09 | ban10 | ban12 | ban13 | ban14 | ban15 | ban16 | ban17 | ban18 | ban19 | ban20 | ban20 | ban21)

HOST=10.65.48.199
USER=$1
PASSWD=xxxxxxx

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
#print -p cd directory
##print -p binary
print -p prompt
print -p ascii
print -p mput $1*$2*
print -p bye

wait
exit 0
;;
*)
Message=”Usage: sendtoftp username date.”
Message1=”Example: sendtoftp ban12 09012010.”
echo ” ”
echo ” ”
echo $Message ” “$Message1
echo ” ”
echo ” ”
;;
esac
fi
}