LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script for time (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-for-time-4175485262/)

sandeep117 11-20-2013 10:09 AM

bash script for time
 
Hello Friends,

How to create a script which would print time to a file every 10 seconds for an hour ?

druuna 11-20-2013 10:43 AM

Quote:

Originally Posted by sandeep117 (Post 5067914)
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.

pauldean 11-20-2013 07:40 PM

How about a one liner?
 
Quote:

Originally Posted by sandeep117 (Post 5067914)
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

jamison20000e 11-20-2013 09:19 PM

Hello.
Quote:

Originally Posted by sandeep117 (Post 5067914)
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 (Post 5068110)
let start=$SECONDS; while [ $(($SECONDS-$start)) -lt 3600 ]; do date; sleep 10; done


good luck and have fun!

Firerat 11-21-2013 12:05 AM

Quote:

Originally Posted by sandeep117 (Post 5067914)
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


kooru 11-21-2013 01:06 AM

Code:

for n in `seq 360`; do date >> FILE; sleep 10; do

sandeep117 11-21-2013 01:57 AM

Thank You for prompt replies !

jpollard 11-21-2013 07:48 AM

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.

jamison20000e 11-21-2013 07:54 AM

I guess (showing :Dff) every one missed this "3 members found this post helpful."

colucix 11-21-2013 08:15 AM

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?!?


All times are GMT -5. The time now is 04:47 AM.