Frost Frost - 2 months ago 13
Bash Question

How to execute the same loop for 1 hour in bash/ linux scripting?

I want to open and close some web pages with random intervals for an hour.

So far i have written the innards

FILE=webpages
TIMES=0
while test $TIMES -lt 10
do
#Picks random line(Website) from FILE
TEMPWEB=`shuf -n 1 $FILE`
echo "Opening" $TEMPWEB
#Opens TEMPWEB
xdg-open $TEMPWEB
sleep 10
#Kills firefox
ps -e | grep firefox | sed 's/\(.[0-9]*\).*/\1/' | xargs kill
TIMES=$(($TIMES+1))
done


So I'm am missing the while condition.

I want something like:

TIMER = 0h 0m 0s
while( TIMER < 1 h)
....
done


Is this possible?

PS. what's the command to random number?

Answer

Surely. Try:

#!/bin/bash
START=`date +%s`
while [ $(( $(date +%s) - 3600 )) -lt $START ]; do
    ....
done

date +%s shows the current time in seconds since 1970. The loop computes the current date of an hour ago and checks if it exceeds the start time.