Code: Select all
#!/bin/sh
# See Usage about this script.
# Author: Sascha Wuestemann
# Date : 2007-08-15
# Email : develop@killerhippy.de
Usage () {
echo "$0 needs the unix timestamp"
echo "and converts it to human readable date."
}
function CheckInput() {
# should be a positive integer
# check if we have got anything
test -z "$1" && return 1
# check if we have an integer
echo "$1" | egrep -q '^[[:space:]]*[+-]?[[:space:]]*[[:digit:]]+[[:space:]]*$' || return 2
# check if it is in limits, unused here
#[ "$1" -lt "-10" -o "$1" -gt "+10" ] && return 3;
# check if it greater 0 instead
[ "$1" -gt "0" ] || return 3;
return 0
# Thanks to Linuxfibel, see
# http://www.linuxfibel.de/bashprog.htm
}
function GetInput() {
# This is called once only if there was no argument given.
# Then we might get it while running
echo -n "Give unix timestamp:"
read INPUT
# Keep running only if we have got an integer greater zero
CheckInput $INPUT && Execute $INPUT || exit 1
}
function Execute() {
date -d "1970-01-01 $1 sec" "+%Y.%m.%d %H:%M:%S"
# Thanks to Ingo Blechschmidt, see
# http://www.pro-linux.de/news/2004/7199.html
}
# uncomment the following line for debugging
#set -xv
INPUT=$1
CheckInput $INPUT
# The follwing cases are:
# 1 empty argument or input.
# 2 argument is no integer
# 3 argument is not greater zero.
# 0 argument is an integer and greater zero.
case $? in
1) Usage;
GetInput;
;;
2) Usage;
exit 1;
;;
3) Usage;
exit 1;
;;
0) Execute $INPUT;
exit 0;
;;
esac
exit 0