Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash variable doubles in value - why?

I have a simple shell script set up to capture images every X seconds. For some reason the value of X seems to double each time through the loop.

#!/bin/bash

# basic setup for time-lapse

SECONDS=1

while true
do
    DATE=$(date +"%Y-%m-%d_%H%M%S")
    filename=${DATE}_img.jpg
#   fswebcam -r 1280x720  --timestamp "%a %Y-%b-%d %H:%M (%Z)"  /home/pi/JPGS/$filename
    date
    echo "pausing for ${SECONDS} seconds"
    sleep $SECONDS
    date
    echo "====="
done

This is the output I get. The value of SECONDS is not manipulated inside the loop, so I'm confused with what is happening here. Also, the original interval was 30 seconds, I changed it to 1 seconds for testing purposes, and the date calls are for testing/debugging too.

Sun Mar  3 17:51:57 CST 2019
pausing for 1 seconds
Sun Mar  3 17:51:58 CST 2019
=====
Sun Mar  3 17:51:58 CST 2019
pausing for 2 seconds
Sun Mar  3 17:52:00 CST 2019
=====
Sun Mar  3 17:52:00 CST 2019
pausing for 4 seconds
Sun Mar  3 17:52:04 CST 2019
=====
Sun Mar  3 17:52:04 CST 2019
pausing for 8 seconds
Sun Mar  3 17:52:12 CST 2019
=====
Sun Mar  3 17:52:12 CST 2019
pausing for 16 seconds
Sun Mar  3 17:52:28 CST 2019
=====
Sun Mar  3 17:52:28 CST 2019
pausing for 32 seconds
Sun Mar  3 17:53:00 CST 2019
=====
Sun Mar  3 17:53:00 CST 2019
pausing for 64 seconds
Sun Mar  3 17:54:04 CST 2019
=====
Sun Mar  3 17:54:04 CST 2019
pausing for 128 seconds

What am I missing here?

This is under a Raspberry Pi

like image 512
Levon Avatar asked Aug 12 '26 17:08

Levon


1 Answers

Pick a different name for $SECONDS.

$SECONDS is a built-in shell variable. It expands to the number of seconds since the shell was started.

From the Bash manual:

'SECONDS'

This variable expands to the number of seconds since the shell was started. Assignment to this variable resets the count to the value assigned, and the expanded value becomes the value assigned plus the number of seconds since the assignment.

like image 157
Keith Thompson Avatar answered Aug 14 '26 08:08

Keith Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!