Skip to content

Instantly share code, notes, and snippets.

@raidzero
Created March 3, 2014 00:38
Show Gist options
  • Save raidzero/9316404 to your computer and use it in GitHub Desktop.
Save raidzero/9316404 to your computer and use it in GitHub Desktop.
Get last boot time
#!/bin/sh
# get the last boot time and display it in 12 hour format
# USAGE: lastboot.sh [date|time|both]
LASTBOOT_LINE=`last | grep reboot | head -n 1`
DATE=`echo $LASTBOOT_LINE | awk '{print$5,$6,$7}'`
TIME=`echo $LASTBOOT_LINE | awk '{print$8}'`
#convert to 12 hour
HOUR=`echo $TIME | cut -d : -f1`
MIN=`echo $TIME | cut -d : -f2`
if [ $HOUR -ge 12 ] && [ $HOUR -le 23 ]; then
AMPM="PM"
let HOUR=$HOUR-12
elif [ $HOUR -ge 1 ] && [ $HOUR -le 11 ]; then
AMPM="AM"
HOUR=$HOUR
fi
if [ $HOUR -eq 0 ]; then
AMPM="AM"
HOUR=12
fi
TIME="$HOUR:$MIN $AMPM"
if [ "$1" == "both" ]; then
echo "$DATE $TIME"
elif [ "$1" == "time" ]; then
echo "$TIME"
elif [ "$1" == "date" ]; then
echo "$DATE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment