60 second Timer with Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 60 second Timer with Shell Script
# 1  
Old 10-03-2009
Java 60 second Timer with Shell Script

Please how can i display a 60 second active countdown timer in an echo message.
# 2  
Old 10-03-2009
e.g.
Code:
#!/bin/ksh
for (( i=60; i>0; i--)); do
  sleep 1 &
  printf "  $i \r"
  wait
done

This work in ksh as well. For posix you probably have to use a while loop.

Last edited by Scrutinizer; 10-04-2009 at 02:56 PM.. Reason: replaced \b\b with \r
# 3  
Old 10-03-2009
Hi

this is not the best solution, but you can improve it.

Code:
input=60
tic=`date +%S`
elap_time=0

while [ "$elap_time" -le "$input" ]; do

toc=`date +%S`
elap_time=`expr $toc - $tic`
done

echo "time elapsed"

EDIT: Sorry I don't answ to your question, becouse you want display it.

Bye

Last edited by Dedalus; 10-03-2009 at 12:28 PM..
# 4  
Old 10-04-2009
Quote:
Originally Posted by Scrutinizer
e.g.
Code:
#!/bin/ksh
printf "  "
for (( i=60; i>0; i--)); do
  sleep 1 &
  printf "\b\b$i"
  wait
done

This work in ksh as well. For posix you probably have to use a while loop.
Works (almost) fine in bash, too. But I don't understand the use of the sleep command as a background process. Furthermore, the "done" messages caused by returning sleeps disturb the display :-(

Nevertheless, much simplier would be just the good ole
Code:
sleep 60; echo "\a"

BR,

pen
# 5  
Old 10-04-2009

Code:
countdown()
{
  countdown=${1:-60}   ## 60-second default
  w=${#countdown}
  while [ $countdown -gt 0 ]
  do
    sleep 1 &
    printf "  %${w}d\r" "$countdown"
    countdown=$(( $countdown - 1 ))
    wait
  done
  printf "\a"
} 2>/dev/null


Last edited by cfajohnson; 10-04-2009 at 12:05 PM..
# 6  
Old 10-04-2009
Quote:
Originally Posted by pen
Works (almost) fine in bash, too. But I don't understand the use of the sleep command as a background process. Furthermore, the "done" messages caused by returning sleeps disturb the display :-(
Hi Pen, the sleep in the background ensures that you use as little time as possible processing the other commands in the loop. Otherwise total sleep is sometimes a bit more than 60 seconds.
You'll get done messages if you execute it from the command line, not if you execute it from a script. Alternatively you can enclose it in parentheses like so:
Code:
(for (( i=60; i>0; i--)); do
  sleep 1 &
  printf "  $i \r"
  wait
done)

Then you won't get the pesky job status.
Or you can put it in a function like CFA suggests..
(BTW I just changed the \b\b for \r because now I realise the backspaces weren't working properly).
Quote:
Nevertheless, much simplier would be just the good ole
Code:
sleep 60; echo "\a"

BR,

pen
Indeed, but it is my understanding that the OP requested a countdown timer.

Last edited by Scrutinizer; 10-04-2009 at 02:55 PM..
# 7  
Old 10-04-2009
Code:
i=60;while [ $i -gt 0 ];do if [ $i -gt 9 ];then printf "\b\b$i";else  printf "\b\b $i";fi;sleep 1;i=`expr $i - 1`;done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help please with FTP timer script

Hi Could somebody please help me with this code? ------------------------------------------ @ftp -i -s:"%~f0"&GOTO:EOF open mysite.co.uk myusername mypassword !:--- FTP commands below here --- cd public_html/ binary lcd c:\ put myfile.jpg bye ... (3 Replies)
Discussion started by: dobbies
3 Replies

2. Shell Programming and Scripting

Looping in the shell script with help of script timer.

Hello Experts- We are facing some issues in the while loop script when we use the script time to decide whether to exist from the loop or continue. Below is the script SrcExitLoop="FALSE" Src_InitialStartTime=`date +%s` Src_StartTime=`date +%s` Src_NUM_ALERTS=0 TOTAL_ALERTS=`expr <SOME... (4 Replies)
Discussion started by: Amey Joshi
4 Replies

3. UNIX for Dummies Questions & Answers

Shell Script / Timer

Hey folks, We already got a working Script running, but actually we gotta start it manually but at least we want to run it like every 30minutes 1time. Could anyone give it a shot? #!/bin/sh for i in A B C D E F G H I J K L M N O P Q R S T U V W X Z do ln -sv /x/$i/*/PDF/001-*.*... (1 Reply)
Discussion started by: Yjerith
1 Replies

4. Shell Programming and Scripting

Perl script timer on program launch

So I am attempting to get a short but complex perl script to be able to time out an application that I will launch from a command line. The terminal I use is MKS C SHELL and I am having trouble doing the a job spawn that will launch the application and keep time on it. I know you could do this... (1 Reply)
Discussion started by: vas28r13
1 Replies

5. UNIX for Dummies Questions & Answers

Timer

is there a timer function in unix without using C? for example i want to display a message after 5 seconds how do i do that? (2 Replies)
Discussion started by: khestoi
2 Replies

6. Shell Programming and Scripting

Startup Script "run process with Timer"

Hi I have a script that execute every X minute for checking new files in a folder and converting to pdf. Is there any way to start this script automatically on linux startup?. I use sleep function in script with infinite loop. while do killall -u `whoami` -q soffice soffice... (0 Replies)
Discussion started by: zawmn83
0 Replies

7. Shell Programming and Scripting

Timer

Is there a way to make a timer? E.g Please give the seconds... ... (6 Replies)
Discussion started by: aekaramg20
6 Replies

8. Shell Programming and Scripting

Simple script? First timer...

Hey guys, I'm brand new to the bash scripting world, here's my issue.. When people SSH to my box, I'm wanting to use screen to monitor them if I so choose. So, I've created a new user: testing, and I've set its startup shell to /bin/myshell.sh whose contents read this: #!/bin/sh $me =... (4 Replies)
Discussion started by: poon
4 Replies

9. UNIX for Dummies Questions & Answers

script run timer

any of you guys have a script for measuring the run time of any given script, where you would sandwich your own script between the timer script or something? thanks! :) (2 Replies)
Discussion started by: mark_nsx
2 Replies

10. Shell Programming and Scripting

timer

Hi all, Wanted to a create a shell script ----------------------------------------------------------------------- 1) which when called will start a timer and wait for 48 hours. after 48 hours it will call some function(say XYZ) 2) Whenever this shell script is called (can be called... (3 Replies)
Discussion started by: k_oops9
3 Replies
Login or Register to Ask a Question