I need a simple script to echo current date 600 times once every second redirect it to a text file and follow the file as it grows using a tail command.

Recommended Answers

All 4 Replies

you've got a problem: prompting date 600 times takes ±2secs.

please be more precise about your request.

what have you tried so far?

Of course he did not say, he would prompt what iit is again, as it does not change as he said date not time. so just store it to variable and track how far the midnight is, and repeat echoing it. (the Date command though has the time also included, so maybe it is required) My cygwin in old Pc gets only 6-7 dates in same second in bash for loop backgrounded and tail -f.the file.

Ok I meant something like this LOL:

#!/bin/sh
dt=`date`
cnt=0;
while [ $cnt -lt 600 ]
do
    echo $dt
    cnt=$(( $cnt + 1 ))
done > dtfile
>outputfile
while :
do
    cat dtfile
    sleep 1
done > outputfile &
sleep 1
tail -f outputfile

Neat! Do you want the date to be updated each time? In your code snippet, you run the 'date' command one time, so each entry will be the same date/time. Try something like this!

#!/bin/sh
for i in $(seq 1 600); do
    date |tee -a outputfile
    sleep 1
done

Or if you're using bash:

#!/bin/bash
for i in {1..600}; do
    date |tee -a outputfile
    sleep 1
done

That should give you the results you need, and print them to the console at the same time it's writing to the output file. If you don't need the date to be current for each iteration, you can just set your date variable first, like in your example:

#!/bin/bash
dt=$(date)
for i in {1..600}; do
    echo $dt |tee -a outputfile
    sleep 1
done

I hope this helps!

commented: Yeah +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.