Here’s a few bash scripts that I’ve cobbled together over the years.

Email Reminders

#!/bin/bash
# Send a reminder to myself

echo "Remember to push the button" | mail -s "Button Reminder" me@brainbot.net
#!/bin/bash
# Send a reminder to a list of email addresses

EMAILLIST=/data/reminders/email-addresses
MESSAGESUBJECT="Monthly Reminder"
MESSAGEBODY=/data/reminders/monthly-reminder-message

for i in $(cat $EMAILLIST) ;
do
  mail -s "$MESSAGESUBJECT" ${i} < $MESSAGEBODY ;
done

Apache 404 Report

#!/bin/bash
# 404 Report: Get the top 50 404s from an Apache combined access log

LOGFILE=/var/log/httpd/brainbot.net/www/access_log

echo "Top 50 missing files for www.brainbot.net"
echo "Count     File"

egrep " 404 " $LOGFILE \
 | awk '{print $7}'    \
 | sort                \
 | uniq -c             \
 | sort -rn            \
 | head -50l

Web Page Generator

#!/bin/bash
# Web Page Generator: Gin up a quick web page so folks
#     can keep an eye on some useful bit of info

TEMPREPORT=/tmp/temp-report.html
FINALREPORT=/home/web/report.html
TIMESTAMP=`date`

echo "<html><head><title>Report</title></head><body>" > $TEMPREPORT

echo "<h1>Report for greatapp</h1>" >> $TEMPREPORT
echo "<h3>Generated on " $TIMESTAMP "</h3>" >> $TEMPREPORT

echo "<p>appserver01 - tomcat thread count (Max 512) - " >> $TEMPREPORT
ssh appserver01 ps -eLf | grep greatapp | wc -l >> $TEMPREPORT
echo "</p>" >> $TEMPREPORT

echo "<p>appserver02 - tomcat thread count (Max 512) - " >> $TEMPREPORT
ssh appserver02 ps -eLf | grep greatapp | wc -l >> $TEMPREPORT
echo "</p>" >> $TEMPREPORT

echo "</body></html>" >> $TEMPREPORT

mv $TEMPREPORT $FINALREPORT

Server Checker

#!/bin/bash
# Server Checker: ssh to a list of servers and run a command

for server in $(cat serverlist); do
  echo -n Checking $server - 
  ssh $server uname -a
done

Log Monitor

#!/bin/bash
# Log Monitor: pulls the last 20 relevent entries and alert if over 90% are a horrible error message

ERRORCOUNT=`grep "relevant log entry" /var/log/appname/foo.log | tail -20l | grep "horrible error message" | wc -l`

if [ "$ERRORCOUNT" -gt "17" ]; then
    echo "/var/log/appname/foo.log error count CRITICAL, restart appname immediately"
    exit 2
else
    echo "/var/log/appname/foo.log OK"
    exit 0
fi

Variations on Test File Creation

#!/bin/bash
# Make 100 copies of a file
for ((i=1; i<101; i++)); do 
  cp picture.jpg picture_$i.jpg;
done
#!/bin/bash
# Create 100 empty test files
for ((i=100; i<200; i++)); do 
  touch $i.txt;
done
#!/bin/bash
# Create 15 files, each with 5 MB of random data
for ((i=1; i<16; i++)); do
  dd if=/dev/urandom of=/tmp/test/$i bs=1024 count=5120
done

Variations on File Renaming

#!/bin/bash
# File Renamer: Takes command-line arguments and renames files appropriately
# Example: filerenamer.sh jpg texas tx
#   This would rename texas-map.jpg to tx-map.jpg

FILENAME=$1
BEFORE=$2
AFTER=$3

for i in $( ls *$FILENAME* ); do
  src=$i;
  gt=$(echo $i | sed -e "s/$BEFORE/$AFTER/");
  mv $src $tgt;
done
#!/bin/bash
# Convert all files in a directory to lower case
for file in * ; do 
  [ -f $file ] && mv -i $file `echo $file | tr '[A-Z]' '[a-z]'`;
done
#!/bin/bash
# Rename all files to a series of incrementing numbers
export counter=1
for file in *.jpg; do
  mv $file "$counter.jpg";
  let "counter+=1";
done
#!/bin/bash
# Rename test.txt to foo-test.txt
for file in *.txt; do
  mv "$file" "foo-$file";
done
#!/bin/bash
# Rename test.txt to test-foo.txt
for file in *.txt; do
  mv "$file" "$(basename "$file" .txt)-foo.txt";
done