Handy Shell Scripts

Over time I've created some handy shell scripts which I use on a regular basis. I will post the sources here. Some scripts are too large to put in this page, or they change too much. The source for them is kept in a CVS repository, including the source for my .bash_profile

Spamtime:Script to find the amount of time spent on SpamAssassin processing. CVS

#!/bin/sh

unset spam_time
for f in `grep seconds /var/log/mail.log.0 | cut -d " " -f 13`
do 
  spam_time="${f} + ${spam_time}"
done
spam_time="${spam_time} 0"
echo $spam_time | bc
			

Metadata:Script to find what Gentoo Packages need metadata.xml files

#!/bin/bash

prefix=/usr/portage
destination=/home/lisa/public_html
filename=pkgneedmeta.txt
gzip=1

printhead() {
  echo "Ebuilds that need metadata.xml files ..." >${destination}/${filename}
  return 0
}
printpkgs() {
  while read cat
  do
    cd "${prefix}/${cat}" 2>/dev/null
    for pkg in *
    do
      [ -d ${pkg} -a ! -f ${pkg}/metadata.xml -a ${pkg} != "timestamp.x" ] && \
      echo "${cat}/${pkg}" >>${destination}/${filename}
    done
  done < ${prefix}/profiles/categories
  return 0
}

doit() {
  printhead
  printpkgs
  [ ${gzip} == 1 ] && gzip -f9 ${destination}/${filename}
  return 0
}
doit
	

Colorise: A bash function to colorise things based on passing flags, such as -fg=white -fg=blue, etc. This was done the hard way, parsing the flags by hand and not with the built in getopt inside bash mainly because I didn't know about it.

PREFIX='\033['
SEP=";"
FG_BLACK="0;30m"
FG_BLUE="0;34m"
FG_GREEN="0;32m"
FG_CYAN="0;36m"
FG_RED="0;31m"
FG_PURPLE="0;35m"
FG_BROWN="0;33m"
FG_LGRAY="0;37m"
FG_WHITE="1;37m"
FG_LBLUE="1;34m"
FG_LGREEN="1;32m"
FG_LCYAN="1;36m"
FG_LRED="1;31m"
FG_PINK="1;35m"
FG_YELLOW="1;33m"
FG_DGRAY="1;30m"

BG_RED=41
BG_GREEN=42
BG_BROWN=43
BG_BLUE=44
BG_PURPLE=45
BG_CYAN=46
BG_GRAY=47

OPT_UNDERLINE=4
OPT_BLINK=5
OPT_REVERSE=7
OPT_CONSEAL=8

DEFAULT="${PREFIX}${FG_LGRAY}"
 

colorise() {
  local flag s= fg= bg= suff="${DEFAULT}" opts= str="${PREFIX}"
     
  for flag in ${*}
  do
    case ${flag} in
      -fg=* )
        fg="\$FG_$(echo "${flag/-fg=/}" | tr [a-z] [A-Z])"
        
      ;;        
      -bg=* )
        bg="\$BG_$(echo "${flag/-bg=/}" | tr [a-z] [A-Z])"
      ;;
      -blink )
        if [ -z ${opts} ]
        then
          opts="${OPT_BLINK}"
        else
          opts="${opts}${SEP}${OPT_BLINK}"
        fi
      ;;
      -underline )
        if [ -z ${opts} ]
        then
          opts="${OPT_UNDERLINE}"
        else
          opts="${opts}${SEP}${OPT_UNDERLINE}"
        fi
      ;;
      -reverse )
        if [ -z ${opts} ]
        then
          opts="${OPT_REVERSE}"
        else
          opts="${opts}${SEP}${OPT_REVERSE}"
        fi
      ;;
      -conseal )
        if [ -z ${opts} ]
        then
          opts="${OPT_CONSEAL}"
        else
          opts="${opts}${SEP}${OPT_CONSEAL}"
        fi
      ;;
      -nodefault )
        suff=''
      ;;
      * )
        if [ -z ${s} ]
        then
          s="${flag}" 
        else
          s="${s} ${flag}"
        fi
      ;;
     esac
  done
  

  bg=$(eval echo ${bg})
  fg=$(eval echo ${fg})
  
  
  if [ -z "${fg}" ]
  then
    echo ${s}
    return
  fi

  if [ -n "${opts}" ]
  then
    str="${str}${opts}${SEP}"
  fi

  if [ -n "${bg}" ]
  then
    str="${str}${bg}${SEP}"
  fi

  str="${str}${fg}"  
 
  #caller is responsible for final formatting (echo -ne)
  echo "${str}${s}${suff}"
  return
}

		

Breadcrumbs ?
  1. SimCity 4 Language Changer
  2. Site Information - Copyright
  3. Journey to a UK Civil Partnership
  4. About Lisa