#!/bin/bash # # Author: Damian Myerscough # Date: 27/02/2008 # Purpose: This little script will select at random a favourite icon # from a directory containing more than one icon # # Note: For random images that only appear once create a pre_icon.txt # file # # Web root WWW_ROOT="/srv/www/htdocs" # Directory with all your favourite icons FAVICON_DIR="/srv/www/favicon" # A text file containing the previous icon (This file is optional) FAVICON_CONFIG="/srv/www/prv_icon.txt" NUM_OF_ICONS=$( ls $FAVICON_DIR | wc -l ); RANDOM_ICON=$( ls $FAVICON_DIR | head -n $[ $RANDOM % $NUM_OF_ICONS + 1 ] | tail -n 1 ); # Check to see if the prv_icon text file exists if [ -e $FAVICON_CONFIG ]; then # Get the previous icon PREVIOUS_ICON=$( cat $FAVICON_CONFIG ); # Make sure we are not dealing with an empty string if [ ! -z $PREVIOUS_ICON ]; then # Randomly pick an icon, if the icon was previously used pick a new one while [ $RANDOM_ICON = $PREVIOUS_ICON -a -e "$FAVICON_DIR/$PREVIOUS_ICON" ]; do RANDOM_ICON=$( ls $FAVICON_DIR | head -n $[ $RANDOM % $NUM_OF_ICONS + 1 ] | tail -n 1 ); done; fi; # Write the new icon to the previous icon file echo $RANDOM_ICON > $FAVICON_CONFIG # Copy the new icon cp "$FAVICON_DIR/$RANDOM_ICON" "$WWW_ROOT/favicon.ico" else # Just randomly select an icon even if we have already had that icon cp "$FAVICON_DIR/$RANDOM_ICON" "$WWW_ROOT/favicon.ico" fi;