Unix Timestamp zu human readable

Post Reply
Message
Author
User avatar
killerhippy
Posts: 529
Joined: 19. May 2000 19:36
Contact:

Unix Timestamp zu human readable

#1 Post by killerhippy »

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
Es gibt keine dumme Fragen!

Killerhippy

User avatar
killerhippy
Posts: 529
Joined: 19. May 2000 19:36
Contact:

#2 Post by killerhippy »

??? Wieso landet das denn nicht in Kommentierte Beispielscripte???
Es gibt keine dumme Fragen!

Killerhippy

User avatar
killerhippy
Posts: 529
Joined: 19. May 2000 19:36
Contact:

#3 Post by killerhippy »

Also, ich würde mich freuen, wenn dieser Thread ins kommentierte Shell-Script umziehen könnte und der andere mit gleichem Script gelöscht.
Es gibt keine dumme Fragen!

Killerhippy

User avatar
Lateralus
prolinux-forum-admin
Posts: 1238
Joined: 05. May 2004 7:35

#4 Post by Lateralus »

Hallo killerhippy. Ich würde vorschlagen, dass du das Skript unter "Kommentierte Beispielskripte" einfach nochmal postest. Danach lösche ich diesen Thread.

Post Reply