Pete6
Posts: 88
Joined: Sat Nov 24, 2012 4:02 pm

Reading variables from a text file into Bash variables

Mon Feb 24, 2020 7:53 pm

I have a text file called "times"

Code: Select all

05:37, 19:37
07:00, 18:14
The numbers refer to astronomical twilight for dawn and dusk (top line) and sunrise and sunset the lower line.

How can I get these into four separate variables in a bash script please.

aRise aSet
sRise sSet

would be ideal names to use for this.

By way of explanation astronomical twilight is defined as the sun being 18 degrees below the horizon (really dark). Civil twilight is when the sun is 6 degrees below the horizon and military twilight is 12 degrees below. Sunset is when the upper limb of the sun is just visible above the horizon ignoring atmospheric anomalies. With atmospheric anomalies it is generally considered to be 50 arcminutes below the horizon or 90.83 degrees below zenith. Sunset is unsurprisingly the revers of sunrise.

Thanks for your help.
My favorite programming language is a soldering iron.

User avatar
DougieLawson
Posts: 38470
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Reading variables from a text file into Bash variables

Mon Feb 24, 2020 8:21 pm

Code: Select all

#!/bin/bash

while IFS="," read -r f1 f2
do
        printf 'Sunrise: %s to sunset: %s\n' "$f1" "$f2"
done << EOF
05:37, 19:37
07:00, 18:14
EOF
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

GlowInTheDark
Posts: 443
Joined: Sat Nov 09, 2019 12:14 pm

Re: Reading variables from a text file into Bash variables

Mon Feb 24, 2020 9:12 pm

This will put them into $1 through $4 - you can copy them elsewhere if you want.

Code: Select all

set -- $(tr -d , < times)
Or you could put them in an array, like this:

Code: Select all

myvalues=($(tr -d , < times))
Then $myvalues will be the first value, ${myvalues[1]} the second, and so on.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Pete6
Posts: 88
Joined: Sat Nov 24, 2012 4:02 pm

Re: Reading variables from a text file into Bash variables

Mon Feb 24, 2020 9:25 pm

Wow! Just like that. Amazing. It works perfectly.

Thank you both.
My favorite programming language is a soldering iron.

Pete6
Posts: 88
Joined: Sat Nov 24, 2012 4:02 pm

Re: Reading variables from a text file into Bash variables

Mon Feb 24, 2020 10:02 pm

Well the first part worked great. Thanks again.

Now my if statement is broken

Code: Select all

# Time in Linux Epoch seconds and apply any adjustments
# Astronomical Dawn Twilight
date --date="$1" +%s
aRise=$date
echo $aRise

# Sunrise
date --date="$3" +%s
sRise=$date
echo $sRise

# Sunset
date --date="$4" +%s
sSet=$date
echo $sSet

# Astronomical Evening Twilight
date --date="$2" +%s
sSet=$date
echo $sSet
now=$(date +%s)         # time now as seconds since epoch

if   (( $aSet -lt $now )); then
   # Twilight
   arg1="Astronomical Twilight: "
   ARG="-ISO 600 -n -ex auto -w 800 -h 600 -tl 60000  -t 2147483647 -o /run/shm/$SCR.jpg"
elif (( $sSet -lt $now )); then
   # Nightime
   arg1="Sunset: "
   ARG="-ISO 800 -dg 3 -co 35 -drc med -ex night -ss 6000000 -n -w 800 -h 600 -tl 60000  -t 2147483647 -o /run/shm/$SCR.jpg"
elif  (( $sRise -lt $now )); then
   # Daytime
   arg1="Sunrise: "
   ARG="-ISO 80 -n -ex auto -w 800 -h 600 -tl 60000 -t 2147483647 -o /run/shm/$SCR.jpg"
elif  (( $aRise -lt $now )); then
   # Dawn
   arg1="Astronomical Dawn : "
   ARG="-ISO 120 -n -ex auto -w 800 -h 600 -ss 450 -tl 60000  -t 2147483647 -o /run/shm/$SCR.jpg"
fi
I get

Code: Select all

./sun: line 50: ((: -lt 1582581569 : syntax error in expression (error token is "1582581569 ")
./sun: line 54: ((: -lt 1582581569 : syntax error in expression (error token is "1582581569 ")
./sun: line 58: ((: -lt 1582581569 : syntax error in expression (error token is "1582581569 ")
./sun: line 62: ((: -lt 1582581569 : syntax error in expression (error token is "1582581569 ")
where lines 50, 54, 58 and 62 are the if and elif statements. bash seems to think the Linux Epoch dates are in the wrong format. This worked earlier.

The variable $ARG is fed to raspistill to make it take an image once a minute. The four different settings are to allow me to compensate for differing light levels. I'm using a Raspi-Cam 2 with a 185 degree lens.

Thanks again
My favorite programming language is a soldering iron.

GlowInTheDark
Posts: 443
Joined: Sat Nov 09, 2019 12:14 pm

Re: Reading variables from a text file into Bash variables

Mon Feb 24, 2020 11:35 pm

1) I'm assuming that you went with my first suggestion - which puts the contents of the file in $1 through $4. That code is not shown in your listing, but I'm guessing that's the idea - that's what you are passing to the "date" command ($1, $2, $3, $4).

2) Why are you using (( ... ))? Shouldn't you be using [ ... ] ? That's the usual way to do tests in the shell language. Anyway, that's the source of the error messages.

3) You aren't calling "date" properly. To get the epoch date (# of seconds) into a variable, you want to be doing something like:

Code: Select all

aRise=$(date -d "$1" +%s)
Note that there's nothing wrong with using --date, but -d is shorter. I always use -d.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Pete6
Posts: 88
Joined: Sat Nov 24, 2012 4:02 pm

Re: Reading variables from a text file into Bash variables

Tue Feb 25, 2020 1:55 am

That worked much better. Thank you. I am really a very bad programmer. However I am slowly moving from worse to bad if you see what I mean.

I am now trying to multiply two things together. All it does is to allow the user to enter offsets in minutes rather than seconds.

AstroDawnSec=AstroDawnMin * 60 // These all end up or are Linux Epoch seconds derived from the original file and converted to +%s from HH:MM

I have tried just about ever permutation I can think of and the web (so far) has not helped me.
My favorite programming language is a soldering iron.

GlowInTheDark
Posts: 443
Joined: Sat Nov 09, 2019 12:14 pm

Re: Reading variables from a text file into Bash variables

Tue Feb 25, 2020 5:26 am

Heh heh. For *that*, you do want to use (( ... ))

I.e., this should work:

Code: Select all

AstroDawnSec=$((AstroDawnMin * 60))
But then you start to realize that shell isn't really supposed to be used as a programming language. It can be pushed and prodded into being one - and bash has gone quite a way towards making it usable, beyond what basic 'sh' provides - but it will always feel like you're fighting it. The syntax shown above, for doing arithmetic in the shell, is a bash extension.

Don't get me wrong; you probably can get through this task using the shell, but you're going to have to do a lot of "man bash". Longer term, you're probably going to want to find and learn a more complete programming language. Many people who use the Raspberry Pi like something called "Python"; you might want to look into it.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

User avatar
jojopi
Posts: 3190
Joined: Tue Oct 11, 2011 8:38 pm

Re: Reading variables from a text file into Bash variables

Tue Feb 25, 2020 6:28 am

Arithmetic expansion with $(()) is a standard POSIX shell feature. Admittedly I think it came from the Korn shell originally, so it will not work in really old Bourne implementations. It only supports integers!

There is no need to convert times and dates to epoch values for comparison. Formats like "HH:MM" can be compared as strings, as long as we use the 24 hour system with leading zeros. Similarly "YYYY-mm-dd".

Instead of trying to parse the output from "sunwait list", we can use "sunwait poll" to ask whether it is currently dark:

Code: Select all

daylight=$(sunwait poll daylight)
twilight=$(sunwait poll astronomical)

if [ $daylight = DAY ]; then
  echo BRIGHT
elif [ $twilight = NIGHT ]; then
  echo DARK
else
  echo TWILIGHT
fi
This is repeating all the calculations, but that is not significant compared to the cost of taking a picture. It neatly avoids the problems where daylight savings makes twilight end after midnight, or you are too far north and twilight does not end at all during June.

I doubt whether the noise level of the Pi camera would allow an exposure long enough to distinguish astronomical twilight.

Pete6
Posts: 88
Joined: Sat Nov 24, 2012 4:02 pm

Re: Reading variables from a text file into Bash variables

Tue Feb 25, 2020 4:10 pm

Thank you both very much. I am making good progress here. My AllSky camera has not fallen over for nearly 24 hours which is progrees indeed.

@GlowInTheDark
The arithmetic syntax worked and multipication now abounds. I think that you are right. I am pushing my luck with a bash script. Learning Python will probably be my way forward. However once I have a working model this should be easier than writing it from scratch.

@jojopi
Thank you for that excellent piece of code. It worked well. I will probably use this method when I re-write in Python. Right now I am simply comparing Epoch times to determine when to switch raspistill arguments.

As a bit of background, I am a 74 year old Brit living in Charlotte North Carolina. I am fully retired and took up amateur astronomy about 3 years ago. It is certain that I shall come asking for more help in the future so, thank you in advance. If there were a way to safely send you the IP and port address of the camera I would but PMs have been disabled and I do not wish to post the IP address in a public forum.
Pete
My favorite programming language is a soldering iron.

Pete6
Posts: 88
Joined: Sat Nov 24, 2012 4:02 pm

Re: Reading variables from a text file into Bash variables

Tue Feb 25, 2020 7:18 pm

@jojopi,

I just re-read your comment about the Raspberry Pi camera. I am using the Pi-cam with an 8 Mega Pixel with Sony IMX219 CMOS image sensor and a 185 degrees lens. This is clearly not up with my ZWO astro camera but then the price was a little different too. I can detect stars down to just below magnitude 2 which is enough to get started. My plan is to stack images at night using ImageMagick so as to raise the magnitude number. If I can see a mag 4 star from my light polluted home then I'll be happy,

Here's the raspistill arguments I am using
Night -ISO 800 -dg 3 -co 35 -drc med -ex night -ss 6000000 -n -w 800 -h 600 -tl 60000 -t 2147483647
Day -ISO 80 -n -ex auto -w 800 -h 600 -tl 60000 -t 2147483647
Twilight -ISO 600 -n -ex auto -w 800 -h 600 -tl 60000 -t 2147483647

The nighttime parameters seem to be about the limit of the IMX216 sensor.

Once I have this thing working properly I shall take it to my astronomy club's observatory at www.chalotteasronomers.org where we have a 4 acre dark sky site and there I hope to find out just what it is capable of. We have two other allsky cameras down there that cost >1000 quid to compare with. It will be interesting to see how good or bad it is in comparison with the other cameras.
My favorite programming language is a soldering iron.

Pete6
Posts: 88
Joined: Sat Nov 24, 2012 4:02 pm

Re: Reading variables from a text file into Bash variables

Sat Feb 29, 2020 4:13 pm

Thank you to all who helped me make my astronomy AllSky Camera work.

It has now been running and stable for a couple of days. I took it to my astronomy club and compared it with two other quite expensive devices. Mine is not as good but it showed up fairly well nonetheless. I can see stars down to magnitude 3.5 at the observatory which is a Bortle 4 darkness site which is good enough for me since the whole device has cost less than $100.

Now I have two problems.
1. The service job that defines the camera exposure only selects the exposure value when it starts up. Thus I need to either reboot the Pi or stop / start or force-reload the service job at least twice a day to switch between day and night camera settings. The switching works well based up the sunrise data but it cannot switch automatically. I wish to make this happen.

2. The camera image renews once per minute but it is necessary to refresh the webpage each time to make the new image appear. Again, I'd love to have this refresh automatically.

I have not posted any code in this post as right now I am just asking for advice. I'll post any and all of the code if asked. I just did not want to clutter up the forum with loads of code.

Thanks again for your help
Pete
My favorite programming language is a soldering iron.

User avatar
DougieLawson
Posts: 38470
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Reading variables from a text file into Bash variables

Sat Feb 29, 2020 5:50 pm

Start a new thread. Post your code and we can take a look at what you're doing.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Pete6
Posts: 88
Joined: Sat Nov 24, 2012 4:02 pm

Re: Reading variables from a text file into Bash variables

Sat Feb 29, 2020 6:25 pm

Thanks Dougie, will do.
My favorite programming language is a soldering iron.

Return to “Beginners”