Register
It is currently Sat Nov 01, 2014 3:43 am

My REALLY simple bash clock


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Aug 10, 2006 3:36 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
My REALLY simple bash clock

Code:
#!/bin/bash
while true;do clear;echo "===========";date +"%r";echo "===========";sleep 1;done


:D :D


Top
 Profile WWW  
 PostPosted: Sat Sep 02, 2006 11:04 am   
User avatar

Joined: Sat Sep 02, 2006 10:19 am
Posts: 13
Location: United Kingdom
Rather than clear the console all the time, you could use echo with escape sequences to keep it printing to the next line.

Code:
#!/bin/bash
while true;do now=$( date +"%r" ) ; echo -en "\r   $now " ;sleep 1;done


Might be more useful if you don't want to risk loosing console output, but not quite as pretty :wink:


Top
 Profile WWW ICQ  
 PostPosted: Wed Mar 18, 2009 11:02 am   

Joined: Fri Dec 02, 2005 7:05 pm
Posts: 5
Location: Concord, NH
Here are a couple of TCL/Tk clocks; one is really simple, the others are a bit longer. You will need the TCL 8.5 package (if you are using a Debian based system) or the equivalent, which contains TCL and Tk.

Code:
#!/usr/bin/wish
# Simple Digital Clock
proc every {ms body} {eval $body; after $ms [info level 0]}         ;# (1)
pack [label .clock -textvar time]                                   ;# (2)
every 1000 {set ::time [clock format [clock sec] -format %H:%M:%S]} ;# (3)


Code:
#!/usr/bin/wish
# Taken from http://wiki.tcl.tk/1011,
# Originally written by Gary J. S.
# Copied, documented and adapted by Brian Masinick
# Analogclock

   set pi {}
   #original width/height of clock
   set size 400
   #update time (NTSC refresh rate is 30 times a sec, PAL is 25 times a second)
   #update 30 times a second so the hand moves smoothly
   set time 33
   #if update is set to 1, then redraw the entire clock, otherwize just the hands
   set resize 1
   set font Helvetica
   #these values are distances relative to the canvas size
   set cirSize 0.95        ;#size of the circle
   #these values are calculated from the center of the circle
   #relative to canvas size
   set inrLength 0.03      ;#where the hands start
   set secLength 0.45      ;#where the second hand ends
   set minLength 0.35      ;#where the minute hand ends
   set hrsLength 0.25      ;#where the hour hand ends
   set tikLength 0.40      ;#where all ticks start
   set tk2Length .43       ;#wehre the small ticks end
   set tk3Length .475      ;#where the big ticks end
   set n1pos 0.45          ;#placement of numbers 1-12
   set n2pos 0.38          ;#placement of numbers 1-60
   #width of hands, relative to canvas size
   set secWidth .015       ;#second hand
   set minWidth .025       ;#minute hand
   set hrsWidth .045       ;#hour hand
   #size of fonts, relative to canvas size
   set fontSize 0.03       ;#1-12
   set fnt2Size 0.02       ;#1-60

   proc main {} {
           #setup global vars
           global pi size
           set pi [pi] ;#save pi so we don't have to call the function everytime

           #setup main window
           wm title . Clock
           #when the window is resized force the clock to be redrawn
           bind . <Configure> {set ::resize 1}

           #create a canvas
           grid [canvas .clock -width $size -height $size -bd 0] -sticky news
           grid rowconfigure . 0 -weight 1
           grid columnconfigure . 0 -weight 1

           #create canvas objects, placement does not matter
           .clock create oval 5 5 195 195 -tags {circle} -fill white
           .clock create line 100 100 100 35 -width 3 -tags {hands hourhand} \
                   -fill green -capstyle round
           .clock create line 100 100 100 25 -width 5 -tags {hands minutehand} \
                   -fill blue -capstyle round
           .clock create line 100 100 100 5  -width 9 -tags {hands secondhand} \
                   -fill red -capstyle round

           #create numbers, 1-12
           for {set i 1} {$i <= 12} {incr i} {
                   .clock create text 0 0 -text $i -tags "n n$i"
           }

           #create ticks, and numbers 1-16
           for {set i 1} {$i <= 60} {incr i} {
                   .clock create line 0 0 10 10 -tags tick$i
                   .clock create text 0 0 -text $i -tags "nn nn$i"
           }

           #start the timer
           updateClock
   }

   proc updateClock {} {
           #globals
           global pi        size      cirSize
           global secLength minLength hrsLength
           global tikLength tk2Length tk3Length
           global inrLength n1pos     n2pos
           global secWidth  minWidth  hrsWidth
           global font      fontSize  resize
           global fnt2Size  time

           #get time
           set msec [string range [clock milliseconds] end-2 end]
           foreach {sec min hour} [clock format [clock seconds] -format "%S %M %I"] {}

           #find relative position
           set sp [expr (([unpad $sec] * 1000) + [unpad $msec]) / 60000.0]
           set mp [expr (((([unpad $min] * 60) + [unpad $sec]) * 1000) + [unpad $msec]) / 3600000.0]
           set hp [expr (([unpad $hour] * 60) + [unpad $min]) / 720.0]

           #get the size of the widget
           set w [winfo width .clock];set cx [expr $w / 2]
           set h [winfo height .clock];set cy [expr $h /2]
           set size [expr $w>$h?$h:$w]

           #if size changed, move numbers, ticks, and circle
           if {$resize} {
                   #move the circle
                   set x1 [expr $cx - round(($size / 2) * $cirSize)]
                   set y1 [expr $cy - round(($size / 2) * $cirSize)]
                   set x2 [expr $cx + round(($size / 2) * $cirSize)]
                   set y2 [expr $cy + round(($size / 2) * $cirSize)]
                   .clock coords circle $x1 $y1 $x2 $y2

                   #move the numbers, 1-12
                   for {set i 1} {$i <= 12} {incr i} {
                           set np [expr $i / 12.0]
                           set x1 [expr $cx + ($size * $n1pos) * sin($np * $pi * 2)]
                           set y1 [expr $cy - ($size * $n1pos) * cos($np * $pi * 2)]
                           .clock coords n$i $x1 $y1
                   }

                   #move the ticks, and numbers 1-60
                   for {set i 1} {$i <= 60} {incr i} {
                           set np [expr $i / 60.0]
                           if {[expr $i % 5]} {
                                   #move the long ticks
                                   set x1 [expr $cx + ($size * $tikLength) * sin($np * $pi * 2)]
                                   set y1 [expr $cy - ($size * $tikLength) * cos($np * $pi * 2)]
                                   set x2 [expr $cx + ($size * $tk3Length) * sin($np * $pi * 2)]
                                   set y2 [expr $cy - ($size * $tk3Length) * cos($np * $pi * 2)]
                                   .clock coords tick$i $x1 $y1 $x2 $y2
                           } else {
                                   #move the short ticks
                                   set x1 [expr $cx + ($size * $tikLength) * sin($np * $pi * 2)]
                                   set y1 [expr $cy - ($size * $tikLength) * cos($np * $pi * 2)]
                                   set x2 [expr $cx + ($size * $tk2Length) * sin($np * $pi * 2)]
                                   set y2 [expr $cy - ($size * $tk2Length) * cos($np * $pi * 2)]
                                   .clock coords tick$i $x1 $y1 $x2 $y2
                           }
                           #move second set of numbers
                           set x1 [expr $cx + ($size * $n2pos) * sin($np * $pi * 2)]
                           set y1 [expr $cy - ($size * $n2pos) * cos($np * $pi * 2)]
                           .clock coords nn$i $x1 $y1
                   }

                   #fonts
                   .clock itemconfigure n -font "$font [expr round($fontSize * $size) * -1]"
                   .clock itemconfigure nn -font "$font [expr round($fnt2Size * $size) * -1]"

                   #resize the hands
                   .clock itemconfigure secondhand -width [expr round($secWidth * $size)]
                   .clock itemconfigure minutehand -width [expr round($minWidth * $size)]
                   .clock itemconfigure hourhand   -width [expr round($hrsWidth * $size)]

                   #don't resize next time
                   set resize 0
           }

           #move the second hand
           set x1 [expr $cx + ($size * $inrLength) * sin($sp * $pi * 2)]
           set y1 [expr $cy - ($size * $inrLength) * cos($sp * $pi * 2)]
           set x2 [expr $cx + ($size * $secLength) * sin($sp * $pi * 2)]
           set y2 [expr $cy - ($size * $secLength) * cos($sp * $pi * 2)]
           .clock coords secondhand $x1 $y1 $x2 $y2

           #move the minute hand
           set x1 [expr $cx + ($size * $inrLength) * sin($mp * $pi * 2)]
           set y1 [expr $cy - ($size * $inrLength) * cos($mp * $pi * 2)]
           set x2 [expr $cx + ($size * $minLength) * sin($mp * $pi * 2)]
           set y2 [expr $cy - ($size * $minLength) * cos($mp * $pi * 2)]
           .clock coords minutehand $x1 $y1 $x2 $y2

           #move the hour hand
           set x1 [expr $cx + ($size * $inrLength) * sin($hp * $pi * 2)]
           set y1 [expr $cy - ($size * $inrLength) * cos($hp * $pi * 2)]
           set x2 [expr $cx + ($size * $hrsLength) * sin($hp * $pi * 2)]
           set y2 [expr $cy - ($size * $hrsLength) * cos($hp * $pi * 2)]
           .clock coords hourhand $x1 $y1 $x2 $y2

           #lets do it again sometime
           after $time updateClock
   }

   #short proc to remove the leading 0 from numbers
   #and make sure an int is returned
   proc unpad {int} {
           regsub ^0 $int "" int1
           if {![string is int $int1]} {
                   return 0
           }
           if {![string length $int1]} {
                   return 0
           }
           return $int1
   }

   #proc to return value of pi
   proc pi {} {expr acos(-1)}

   main


Code:
#!/usr/bin/wish
# German analog clock,  but works for English speaking people too
package require Tk

canvas .uhr -width 200 -height 200 -highlightthickness 0
wm geometry .  +[expr {[winfo screenwidth .]-[winfo reqwidth .]}]+0

pack .uhr
bind .uhr <Double-Button-1> {
     if {[expr {![wm overrideredirect .]}]} {
         wm overrideredirect . yes
         # .uhr configure -background SystemBackground
     } else {
         wm overrideredirect . no
         # .uhr configure -background SystemButtonFace
     }
}

set PI [expr {asin(1)*2}]
set sekundenzeigerlaenge 85
set minutenzeigerlaenge  75
set stundenzeigerlaenge  60

proc drawClock {} {
     global PI
     global sekundenzeigerlaenge
     global minutenzeigerlaenge
     global stundenzeigerlaenge
     set aussenradius 95.0
     set innenradius  83.0
     # Ziffernblatt
     .uhr create oval 5 5 195 195 -fill white -outline ""
     # Zeiger
     .uhr create line 100 100     [expr {100+$stundenzeigerlaenge}] 100 -tag stundenschatten
     .uhr create line 100 100 100 [expr {100-$minutenzeigerlaenge}]     -tag minutenschatten
     .uhr create line 100 100 100 [expr {100+$sekundenzeigerlaenge}]    -tag sekundenschatten
     .uhr create line 100 100     [expr {100+$stundenzeigerlaenge}] 100 -tag {stundenzeiger zeiger}
     .uhr create line 100 100 100 [expr {100-$minutenzeigerlaenge}]     -tag {minutenzeiger zeiger}
     .uhr create line 100 100 100 [expr {100+$sekundenzeigerlaenge}]    -tag {sekundenzeiger zeiger}
     .uhr itemconfigure stundenzeiger    -width 8
     .uhr itemconfigure minutenzeiger    -width 4
     .uhr itemconfigure sekundenzeiger   -width 2 -fill red
     .uhr itemconfigure stundenschatten  -width 8 -fill gray
     .uhr itemconfigure minutenschatten  -width 4 -fill gray
     .uhr itemconfigure sekundenschatten -width 2 -fill gray
     # Ziffern
     for {set i 0} {$i < 60} {incr i} {
         set r0 [expr {$innenradius + 5}]
         set r1 [expr {$innenradius +10}]
         set x0 [expr {sin($PI/30*(30-$i))*$r0+100}]
         set y0 [expr {cos($PI/30*(30-$i))*$r0+100}]
         set x1 [expr {sin($PI/30*(30-$i))*$r1+100}]
         set y1 [expr {cos($PI/30*(30-$i))*$r1+100}]
         if {[expr {$i%5}]} {
         }
     }
     for {set i 0} {$i < 12} {incr i} {
         set x [expr {sin($PI/6*(6-$i))*$innenradius+100}]
         set y [expr {cos($PI/6*(6-$i))*$innenradius+100}]
         .uhr create text $x $y \
                 -text [expr {$i ? $i : 12}] \
                 -font {Helvetica 13 bold} \
                 -fill #666666 \
                 -tag ziffer
     }
     wm resizable . no no
}

proc stundenZeigerAuf {std} {
     global PI
     global stundenzeigerlaenge
     set x0 100
     set y0 100
     set dx [expr {sin ($PI/6*(6-$std))*$stundenzeigerlaenge}]
     set dy [expr {cos ($PI/6*(6-$std))*$stundenzeigerlaenge}]
     set x1 [expr {$x0 + $dx}]
     set y1 [expr {$y0 + $dy}]
     .uhr coords stundenzeiger $x0 $y0 $x1 $y1
     set schattenabstand 3
     set x0s [expr {$x0 + $schattenabstand}]
     set y0s [expr {$y0 + $schattenabstand}]
     set x1s [expr {$x1 + $schattenabstand}]
     set y1s [expr {$y1 + $schattenabstand}]
     .uhr coords stundenschatten $x0s $y0s $x1s $y1s
}

proc minutenZeigerAuf {min} {
     global PI
     global minutenzeigerlaenge
     set x0 100
     set y0 100
     set dx [expr {sin ($PI/30*(30-$min))*$minutenzeigerlaenge}]
     set dy [expr {cos ($PI/30*(30-$min))*$minutenzeigerlaenge}]
     set x1 [expr {$x0 + $dx}]
     set y1 [expr {$y0 + $dy}]
     .uhr coords minutenzeiger $x0 $y0 $x1 $y1
     set schattenabstand 4
     set x0s [expr {$x0 + $schattenabstand}]
     set y0s [expr {$y0 + $schattenabstand}]
     set x1s [expr {$x1 + $schattenabstand}]
     set y1s [expr {$y1 + $schattenabstand}]
     .uhr coords minutenschatten $x0s $y0s $x1s $y1s
}

proc sekundenZeigerAuf {sec} {
     global PI
     global sekundenzeigerlaenge
     set x0 100
     set y0 100
     set dx [expr {sin ($PI/30*(30-$sec))*$sekundenzeigerlaenge}]
     set dy [expr {cos ($PI/30*(30-$sec))*$sekundenzeigerlaenge}]
     set x1 [expr {$x0 + $dx}]
     set y1 [expr {$y0 + $dy}]
     .uhr coords sekundenzeiger $x0 $y0 $x1 $y1
     set schattenabstand 5
     set x0s [expr {$x0 + $schattenabstand}]
     set y0s [expr {$y0 + $schattenabstand}]
     set x1s [expr {$x1 + $schattenabstand}]
     set y1s [expr {$y1 + $schattenabstand}]
     .uhr coords sekundenschatten $x0s $y0s $x1s $y1s
}

proc showTime {} {
     after cancel showTime
     after 1000 showTime
     set secs [clock seconds]
     set l [clock format $secs -format {%H %M %S} ]
     wm title . [join $l :]
     set std [lindex $l 0]
     set min [lindex $l 1]
     set sec [lindex $l 2]
     regsub ^0 $std "" std
     regsub ^0 $min "" min
     regsub ^0 $sec "" sec
     set min [expr {$min + 1.0 * $sec/60}]
     set std [expr {$std + 1.0 * $min/60}]
     stundenZeigerAuf $std
     minutenZeigerAuf $min
     sekundenZeigerAuf $sec
}

drawClock
showTime


Top
 Profile WWW YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 7 guests


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 post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP