Shell script: template.sh


Template for a basic shell script. This provides a sample of logging from a shell script.
 

#! /bin/sh
## 2012-11-07 Vik Solem
## template for shell scripts

MYname=`basename $0`
ROOT="${HOME}/${MYname}"
LOGDIR="${ROOT}logs/`date +'%Y-%m'`/"
DATADIR="${ROOT}data/`date +'%Y-%m'`/"
NOW=`date +'%Y-%m-%d_%H-%M-%S'`
LOGFILE=${LOGDIR}${MYname}.log_$$_${NOW}
DATAFILE=${DATADIR}${MYname}.dat_$$_${NOW}
mkdir -p ${ROOT}
mkdir -p ${LOGDIR}
mkdir -p ${DATADIR}

log() {
    echo `date +'%Y-%m-%d_%H-%M-%S'`" $*" | tee -a ${LOGFILE}
}
cmd() {
    log "$*"
    $* 2>&1 | tee -a ${LOGFILE}
}
skip() {
    log "SKIPPING this command: $*"
}
    
log Start

cmd ls
skip cmd rm file-to-delete

log Finish