1834543 Members
3409 Online
110069 Solutions
New Discussion

Re: clock

 
Khashru
Valued Contributor

clock

I want to add a clock in a script. the script is used for showing menu. I want that the time will be changed after 5 sec. how can i do that

System Name : hpdev Time :

Choose one of the following option:

[P] for Password Change
[U] for Unlock user
[E] for Exit
3 REPLIES 3
Muthukumar_5
Honored Contributor

Re: clock

Do you want to change the time after 5 sec eventhough the input is given for Choose or not?

It is easy to change the time with clear command as like,

while [[ 1 ]]
do

echo "System Name: `hostname` Time: `date`"
echo "Choose"
echo "[P] for Password Change
[U] for Unlock user
[E] for Exit"
read OPTION

# Use some function to do operation.
here...()
clear

sleep 5

done


--
Muthu
Easy to suggest when don't know about the problem!
Elmar P. Kolkman
Honored Contributor

Re: clock

You can use this as a base for what you want. Mind that it will 'destroy' old traps you set !

But it will print out the date every 5 seconds.

#!/bin/ksh
trap '/usr/bin/date' HUP

( while true
do
kill -HUP $$
sleep 5
done
) &

ss=$!

trap '/usr/bin/kill $ss' INT

read a

kill $ss

Every problem has at least one solution. Only some solutions are harder to find.
Bill Hassell
Honored Contributor

Re: clock

If you want the script to respond to the menu commands and at the same time, update the time, the script will be quite tricky to implement. Essentially, you would wait for input for up to 5 seconds, then if nothing was typed, redisplay the header which includes the date. Be sure to use $(date) rather than the obsolete grave accents `date` as it is very easy to mistype the characters. The date command has many options to show just hour:min or add secs, etc. To timeout the read command, see the man page for line.


Bill Hassell, sysadmin