LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-20-2013, 11:09 AM   #1
sandeep117
LQ Newbie
 
Registered: Nov 2013
Location: India
Distribution: ubuntu 13.04
Posts: 4

Rep: Reputation: Disabled
bash script for time


Hello Friends,

How to create a script which would print time to a file every 10 seconds for an hour ?
 
Old 11-20-2013, 11:43 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by sandeep117 View Post
How to create a script which would print time to a file every 10 seconds for an hour ?
What have you tried and what is giving you problems?

Hints: Have a look at the date and sleep commands.
 
3 members found this post helpful.
Old 11-20-2013, 08:40 PM   #3
pauldean
LQ Newbie
 
Registered: Nov 2013
Posts: 11

Rep: Reputation: Disabled
Talking How about a one liner?

Quote:
Originally Posted by sandeep117 View Post
Hello Friends,

How to create a script which would print time to a file every 10 seconds for an hour ?
let start=$SECONDS; while [ $(($SECONDS-$start)) -lt 3600 ]; do date; sleep 10; done
 
1 members found this post helpful.
Old 11-20-2013, 10:19 PM   #4
jamison20000e
Senior Member
 
Registered: Nov 2005
Location: ...uncanny valley... infinity\1975; (randomly born:) Milwaukee, WI, US, Earth, end border$! ◣◢┌∩┐ Fe26-E,e...
Distribution: any GPL that works well on my cheapest; has been KDE or CLI but open... http://goo.gl/NqgqJx &c ;-)
Posts: 3,288
Blog Entries: 2

Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
Wink

Hello.
Quote:
Originally Posted by sandeep117 View Post
Hello Friends,

How to create a script which would print time to a file every 10 seconds for an hour ?
Quote:
Originally Posted by pauldean View Post
let start=$SECONDS; while [ $(($SECONDS-$start)) -lt 3600 ]; do date; sleep 10; done
good luck and have fun!
 
Old 11-21-2013, 01:05 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by sandeep117 View Post
Hello Friends,

How to create a script which would print time to a file every 10 seconds for an hour ?
Code:
#!/bin/bash
for i in {1..360};do
    echo time >> "a file"
    sleep 10
done
 
1 members found this post helpful.
Old 11-21-2013, 02:06 AM   #6
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
Code:
for n in `seq 360`; do date >> FILE; sleep 10; do
 
1 members found this post helpful.
Old 11-21-2013, 02:57 AM   #7
sandeep117
LQ Newbie
 
Registered: Nov 2013
Location: India
Distribution: ubuntu 13.04
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank You for prompt replies !
 
Old 11-21-2013, 08:48 AM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
Note: None of the solutions is accurate.

The problem is that though the sleep is accurate, it doesn't account for the delay caused by writing the data to the file. Things like how busy the system is, memory pressure all affecting paging, which will slow things down..

Now it can be sufficient... but over an hour the time will drift by several seconds. So it depends on what you are trying to do as to how well it works.
 
1 members found this post helpful.
Old 11-21-2013, 08:54 AM   #9
jamison20000e
Senior Member
 
Registered: Nov 2005
Location: ...uncanny valley... infinity\1975; (randomly born:) Milwaukee, WI, US, Earth, end border$! ◣◢┌∩┐ Fe26-E,e...
Distribution: any GPL that works well on my cheapest; has been KDE or CLI but open... http://goo.gl/NqgqJx &c ;-)
Posts: 3,288
Blog Entries: 2

Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
I guess (showing ff) every one missed this "3 members found this post helpful."
 
Old 11-21-2013, 09:15 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Oh well, why to wait 1 hour when you can do it in 1 second?
Code:
for i in {0..3600..10}; do date -d "00:00 $i seconds"; done
Anyway, responses would be more helpful if you'd describe your requirements in more details. Or is it just a homework question?!?
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Each time a script is called. A log file is created with time and date + Bash Script. y0_gesh Programming 6 08-17-2012 04:16 AM
time count bash script help mad_penguin Programming 1 06-08-2010 10:12 AM
Need help in Bash script on Time entries manya Programming 14 08-26-2009 02:41 PM
Can I Have Some Help Writing This Bash Script Since Is My First Time Doing One? fiona79us Linux - Newbie 12 05-02-2008 11:53 AM
time a process within bash script aubrey-calm2 Linux - General 2 06-15-2007 02:39 PM


All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration