简体   繁体   中英

Bash - Calculating the time until zero for a queue

I am trying to write a simple script that helps me estimate the time left in an entry queue. So far I have gotten this, but it seems to deliver entirely random results, especially as the time in queue gets longer, or the position gets closer to zero.

To clarify, the script prompts for the initial position 'inum' whenever the queue moves, the user is prompted to enter the current place in queue. (There is no constant movement) I wanted to calculate the time until entry by taking the current place in queue, comparing it to the total amount of people, and then use the elapsed time in queue as a means to estimate a remaining time (+ a 5 Minute grace period). However, this gives me very inconsistent results.

#!/bin/sh
read -p "Users in queue: " inum;
echo ""
n=$inum
t0=$(date +%s)
while true; do
    read -p "Users in queue: " nleft;
    t=$(date +%s)
    d=$((t - t0))
    cal=$(echo "((($d * $nleft / ($n - $nleft)) - $d) / 60) + 5" | bc)
    echo "Time in queue: $((d / 60)) Minutes."
    echo "Approximate time left: $cal Minutes."
    echo ""
done
echo "End."

with time command, use

time yourscript.sh

or with

start=`date +%s`
read -p "Users in queue: " nleft; #into while
end=`date +%s`
caltime=$( echo "$end - $start" | bc -l )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM