Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[bash] Rechnen mit Datum und Uhrzeit?
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Mon Mar 02, 2009 10:06 am    Post subject: [bash] Rechnen mit Datum und Uhrzeit? Reply with quote

Hallo Zusammen,

ich habe mir ein kleines Script gebastelt, das nachts per cronjob startet und diverse Arbeiten erledigt (Updates, Sicherungen, usw.).
Nun möchte ich aber gerne wissen, wie lange dazu gebraucht wird.

Im Moment sieht das so aus:

Code:
echo "beginn $date" > begin.log

--> Erledige o.g. Aufgaben... <--

echo "end $date" > end.log


Wie kann ich nun den Inhalt von begin.log und end.log vergleichen und daraus die Differenz ermitteln?

Das Ergebnis sollte dann z.B. so aussehen: Dauer: 22 min 12 sek

BTW:
Es muss nicht unbedingt so gemacht werden, ich bin über jeden Lösungsvorschlag dankbar.
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3943
Location: Hamburg

PostPosted: Mon Mar 02, 2009 10:29 am    Post subject: Reply with quote

Code:
date +%s
ist dein Freund und :
Code:
date -d @111111111
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Mon Mar 02, 2009 11:40 am    Post subject: Reply with quote

Thx für den Tipp.

Ich habe es jetzt mal so gelöst:


Code:
START="$date +%s"

--> Erledige o.g. Aufgaben... <--

END="$date +%s"

(( DAUER= $END - $START ))

echo 'Dauer: "$((DAUER /3600)) Std $((DAUER % 3600 /60)) Min $((DAUER % 60)) Sek"' >> udate.log


Was jetzt noch fehlt währe die Ausblendung von Stunden, wenn diese gleich Null sind.

Bsp:

22 Min 35 Sek statt 0 Std 22 Min 35 Sek

--> Aber das ist nur Kosmetik.... :lol:
Back to top
View user's profile Send private message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2617
Location: Konradsreuth (Germany)

PostPosted: Mon Mar 02, 2009 12:49 pm    Post subject: Reply with quote

Vielleicht helfen dir die beiden, benutz ich in nem Script von mir:
Code:
# Transform a timestamp to a date

function formatTime
{
   awk "BEGIN { print strftime(\"%Y-%m-%d %H:%M:%S %Z\", \"$1\") }"
}

# Transform seconds to days, hours, minutes and seconds

function printSeconds
{

   # To make it work if we didn't have any downtime ;-)
   if [[ ! $1 ]]; then
      allSecs=0
   else
      allSecs=$1
   fi

   days=$(($allSecs / 86400))
   hours=$((($allSecs / 3600) - (days * 24)))
   minutes=$((($allSecs / 60) - (days * 1440) - (hours * 60)))
   seconds=$(($allSecs % 60))

   out=""

   if [[ $days -gt 0 ]]; then
      out="$days day"
      if [[ $days -gt 1 ]]; then out="${out}s"; fi
   fi

   if [[ $hours -gt 0 ]]; then
      if [[ "$out" != "" ]]; then out="$out, "; fi
      out="$out$hours hour"
      if [[ $hours -gt 1 ]]; then out="${out}s"; fi
   fi

   if [[ $minutes -gt 0 ]]; then
      if [[ "$out" != "" ]]; then out="$out, "; fi
      out="$out$minutes minute"
      if [[ $minutes -gt 1 ]]; then out="${out}s"; fi
   fi

   if [[ $seconds -gt 0 ]]; then
      if [[ "$out" != "" ]]; then out="$out, "; fi
      out="$out$seconds second";
      if [[ $seconds -gt 1 ]]; then out="${out}s"; fi
   fi

   if [[ "$out" == "" ]]; then
      out="0 seconds"
   fi

   echo -n $out

}
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6780

PostPosted: Mon Mar 02, 2009 2:51 pm    Post subject: Reply with quote

Ein Alternative wäre die Benutzung des time-Kommandos.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum