First of all, I’m a beginner on shell-script. This code I’ve done is not working.
I want to repeat a code for 30 seconds but it doesn’t work. It just keep doing my logic indefinitely.
DIFF=0
while [ $DIFF < 30 ]; do
START=$(date +%s)
######## My logic #########
DIFF=$(( $END - $START ))
echo $DIFF
cd ..
sleep 5s
done
I think it’s because I’m not doing the while clause properly?
Well, you definitely need to provide some values for
$STARTand$END. They won’t set themselves!You may want to do something like
to set it to a time in seconds. Of course
ENDwill need to be set inside your loop to get it updated.EDIT:
cd ..is hopefully not really what you plan to run inside the loop. Within a few milliseconds your current directory will be the root directory, with little else accomplished. It would be cheaper to do a singlecd /.EDIT 2: This shouldn’t be such a hard problem. For this edit, I’ve built and tested a one-line solution:
That will correctly update its variables and display them… and it ends after 30 seconds.