Saturday, August 18, 2012

Shell Script Timestamps

Shell Script Trick No. 3 Timestamps
Over the years I've had the need to use time stamps in my scripts to name log files or directories. I created this function to make the task easier.
Usage is as follows:
timestamp "yyyymmdd"
sets the variable $timestamp to 20120818

timestamp "mm/dd/yy"
sets the varible $timestamp to 08/18/2012


##################
# timestamp: This function generates a timestamp
# Calling Profile: timestamp "format"
# Returns:  timestamp
##################
timestamp () {
  format=$1
    yyyy=`date +%Y`
    yy=`date +%y`
    month=`date +%m`
    day=`date +%d`
    hour=`date +%H`
    min=`date +%M`
    sec=`date +%S`

case $format in
     "yesterday yyyy.mm.dd" ) timestamp=`date --date yesterday "+%Y.%m.%d"` ;;
     "yesterday yyyy-mm-dd" ) timestamp=`date --date yesterday "+%Y-%m-%d"` ;;
     "yesterday yyyymmdd" )   timestamp=`date --date yesterday "+%Y%m%d"` ;;
     "yyyy" )                 timestamp="$yyyy" ;;
     "yy" )                   timestamp="$yy" ;;
     "dd" )                   timestamp="$day" ;;
     "mm" )                   timestamp="$month" ;;
     "hh:mm:ss" )             timestamp="$hour:$min:$sec" ;;
     "hhmmss" )               timestamp="$hour$min$sec" ;;
     "mm/dd/yy" )             timestamp="$month/$day/$yy" ;;
     "mm/dd/yyyy" )           timestamp="$month/$day/$yyyy" ;;
     "yyyymmdd"    )          timestamp="$yyyy$month$day" ;;
     "yyyymmddhhmm" )         timestamp="$yyyy$month$day$hour$min" ;;
     "yyyymmddhhmmss" )       timestamp="$yyyy$month$day$hour$min$sec" ;;
     "yyyy-mm-dd" )           timestamp="$yyyy-$month-$day" ;;
     "yyyy.mm.dd" )           timestamp="$yyyy.$month.$day" ;;
     "mmddyyyy"   )           timestamp="$month$day$yyyy" ;;
     "dd/mm/yy"   )           timestamp="$day/$month/$yy" ;;
     "dd-mm-yy"   )           timestamp="$day-$month-$yy" ;;
     "dd-mm-yyyy" )           timestamp="$day-$month-$yyyy" ;;
     "mm-dd-yy"   )           timestamp="$month-$day-$yy" ;;
     "mm-dd-yyyy" )           timestamp="$month-$day-$yyyy" ;;
     "mm dd, yyyy" )          timestamp="$month $day, $yyyy" ;;
             *   )            timestamp="$hour$min$sec";;
esac 
}

1 comment:

  1. Free Online Slot【WG】Play for free online here at stillcasino
    Looking for new players to play free casino slots? 더킹카지노 Now, you can:✔️ Play the newest free casino slots games✔️ Casino bonuses✔️ Exclusive free matchpoint spins. カジノ シークレット

    ReplyDelete