views:

19

answers:

1

How can I send some specific number of, say 1000, messages (echo) per second to a file by a shell script?

i.e. I want to echo 1000 such messages ("hi") per sec to a (tmp) file. echo "hi" >> tmp

A: 

Best you can do is this:

while :; do
    i=0
    while [ $i -lt 1000 ]; do
            echo hi >> tmp
            i=$((i+1))
    done
    sleep 1
done

The time to actually execute the commands in the inner while loop is not taken into account, so this will write slightly less messages per second.

schot
Thank you but I have a fundamental question here: What we are doing is, sleeping 1 second between each 1000 messages, how does it ensure that inner while loop is executed in 1 second? Or what I am asking is probably not accurately possible as many things can actually happen with cpu cycles while this is being done. Please correct me if I am wrong.
hari
@hari You are right: You can't be sure and other programs may use CPU cycles. If this solution is good enough depends on the problem you are trying to solve.
schot