[PLUG] Broken Script, Help Please
Ben Koenig
techkoenig at protonmail.com
Wed Jul 23 18:43:01 UTC 2025
-Ben
On Wednesday, July 23rd, 2025 at 11:18 AM, Michael Barnes <barnmichael at gmail.com> wrote:
> On Wed, Jul 23, 2025, 10:50 Ali Corbin ali.corbin at gmail.com wrote:
>
> > Does the script source your env?
> >
> > On Wed, Jul 23, 2025 at 10:47 AM Michael Barnes barnmichael at gmail.com
> > wrote:
> >
> > > On Fri, Jul 18, 2025, 09:56 Michael Barnes barnmichael at gmail.com
> > > wrote:
> > >
> > > > On Wed, Jul 16, 2025 at 11:59 PM Ben Koenig <techkoenig at protonmail.com
> > >
> > > > wrote:
> > > >
> > > > > -Ben
> > > > >
> > > > > On Wednesday, July 16th, 2025 at 11:05 PM, Ben Koenig <
> > > > > techkoenig at protonmail.com> wrote:
> > > > >
> > > > > > On Wednesday, July 16th, 2025 at 9:57 PM, Michael Barnes
> > > > > > barnmichael at gmail.com wrote:
> > > > > >
> > > > > > > I have a script that is supposed to grab a weather feed and return
> > > > > > > the
> > > > > > > current temperature. If grabs the file fine, but does not return
> > > > > > > the
> > > > > > > temperature.
> > > > > > >
> > > > > > > Here is the line that fails:
> > > > > > >
> > > > > > > T=$(grep "<temp_f>" KEUG.xml |cut -c10- |cut -d '.' -f 1)
> > > > > > >
> > > > > > > Here is the KEUG.xml file:
> > > > > > > <?xml version="1.0" encoding="ISO-8859-1"?>
> > > > > > >
> > > > > > > <?xml-stylesheet href="latest_ob.xsl" type="text/xsl"?>
> > > > > > >
> > > > > > > <current_observation version="1.0" xmlns:xsd="
> > > > > > > http://www.w3.org/2001/XMLSchema" xmlns:xsi="
> > > > > > > http://www.w3.org/2001/XMLSchema-instance"
> > > > > > > xsi:noNamespaceSchemaLocation="
> > > > > > > http://www.weather.gov/view/current_observation.xsd
> > > > > > > "><credit>NOAA's
> > > > > > >
> > > > > > > National Weather Service</credit><credit_URL>https://weather.gov/
> > > > > > >
> > > > > > > </credit_URL><image><url>
> > > > > > > http://forecast.weather.gov/images/xml_badge.png</url><title>NOAA's
> > > > > > >
> > > > > > > National Weather
> > > > > > > Service</title><link>https://www.weather.gov
> > > > > > > </link></image><suggested_pickup>15
> > > > > > >
> > > > > > > minutes after the
> >
> > hour</suggested_pickup><suggested_pickup_period>60</suggested_pickup_period><location>Eugene,
> >
> > > > > > > Mahlon Sweet Field,
> >
> > OR</location><station_id>KEUG</station_id><latitude>44.13333</latitude><longitude>-123.21444</longitude><observation_time>Last
> >
> > > > > > > Update on Jul 16 2025, 7:54 pm
> > > > > > > PDT</observation_time><observation_time_rfc822>Wed, 16 Jul 2025
> > > > > > > 19:54:00
> >
> > -0700</observation_time_rfc822><weather>Fair</weather><temperature_string>88.0
> >
> > > > > > > F (31.1
> >
> > C)</temperature_string><temp_f>88.0</temp_f><temp_c>31.1</temp_c><relative_humidity>26</relative_humidity><wind_string>West
> >
> > > > > > > at 8.1 MPH (7
> >
> > KT)</wind_string><wind_dir>West</wind_dir><wind_degrees>250</wind_degrees><wind_mph>8.1</wind_mph><wind_kt>7</wind_kt><pressure_string>1008
> >
> > mb</pressure_string><pressure_mb>1008</pressure_mb><pressure_in>29.78</pressure_in><dewpoint_string>48.9
> >
> > > > > > > F (9.4
> >
> > C)</dewpoint_string><dewpoint_f>48.9</dewpoint_f><dewpoint_c>9.4</dewpoint_c><heat_index_string>85
> >
> > > > > > > F (30
> >
> > C)</heat_index_string><heat_index_f>85</heat_index_f><heat_index_c>30</heat_index_c><visibility_mi>10.00</visibility_mi><icon_url_base>
> >
> > > > > > > http://forecast.weather.gov/images/wtf/small/
> > > > > > > </icon_url_base><two_day_history_url>
> > > > > > >
> > > > > > > http://forecast.weather.gov/data/obhistory/KEUG.html
> > >
> > > </two_day_history_url><icon_url_name>nskc.png</icon_url_name><ob_url>
> > >
> > > > > > > http://forecast.weather.gov/data/METAR/KEUG.1.txt
> > > > > > > </ob_url><disclaimer_url>
> > > > > > >
> > > > > > > https://www.weather.gov/disclaimer.html
> > > > > > > </disclaimer_url><copyright_url>
> > > > > > >
> > > > > > > https://www.weather.gov/disclaimer.html
> > > > > > > </copyright_url><privacy_policy_url>
> > > > > > >
> > > > > > > https://www.weather.gov/notice.html
> > > > > > > </privacy_policy_url></current_observation>
> > > > > > >
> > > > > > > I should get:
> > > > > > >
> > > > > > > $ echo $T
> > > > > > > 88
> > > > > > >
> > > > > > > Instead, I get:
> > > > > > > $ echo $T
> > > > > > > observation version="1
> > > > > > >
> > > > > > > This has been working for several years. The script runs each
> > > > > > > hour,
> > > > > > > pulling
> > > > > > > the weather statement and culling the current temperature. The
> > > > > > > remainder of
> > > > > > > the script uses the variable to select the appropriate audio file
> > > > > > > to
> > > > > > > announce the current temperature. I set this up about five years
> > > > > > > ago
> > > > > > > and my
> > > > > > > feeble memory has no idea how it really works any more.
> > > > > > >
> > > > > > > Any ideas on what went wrong where?
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Michael
> > > > > >
> > > > > > I think the linebreaks are missing in your datafile. you are using
> > > > > > grep
> > > > > > to search for a specific line and then cut from there, but if newlines
> > > > > > are
> > > > > > removed from the xml then it does what you described.
> > > > > >
> > > > > > XML does not require newlines in order to be valid and it's possible
> > > > > > that the source of your data no longer prints the data with them.
> > > > > >
> > > > > > You will either need to make sure the downloaded xml includes all
> > > > > > the
> > > > > > expected new lines or adjust the script to make sure it doesn't
> > > > > > matter.
> > > > > >
> > > > > > -Ben
> > > > >
> > > > > This might be a more robust one-liner to get the information you need:
> > > > >
> > > > > T=$(awk -F'temp_f>|</temp_f' '{print $2}' KEUG.xml | cut -d. -f1)
> > > >
> > > > This seems to do the trick. Thank you so much.
> > > >
> > > > Michael
> > >
> > > Well, this is strange. I finally got to lay hands on the actual machine
> > > running this. (CENTOS7) Running the script from the command line does not
> > > set the variable, however, running just that line by itself works fine.
> > > Running it on my Mint machine at home works as desired.
> > > I don't understand why the script would not set the variable, but does
> > > work
> > > with just that line.
> > > ???
> > >
> > > Michael
>
>
> Figured it out. I was checking the variable from the command line after the
> script ran. That doesn't work. The variable is only in the script. If I
> stick an "echo $T" in the script, it shows me it works.
> I wish my memory worked better, I should have known this.
>
> However, now I have a new problem. The next line says:
>
> file=tmp0${T}.mp3
>
> and should return
>
> echo $file
> tmp071.mp3
>
> But it returns
>
> tmp0 71.mp3
>
> which, of course, does not work. Again, running them standalone from
> command line works fine, but within the script adds the space.
>
> My brain hurts.
>
> Michael
Looks like that might be a side effect of the fact that awk is parsing line by line. If I run my command as provided earlier, I get couple newlines
<snip>
$ awk -F'temp_f>|</temp_f>' '{print $2}' KEUG.xml | cut -d. -f1
88
</snip>
I can trim that with another grep
<snip>
$ awk -F'temp_f>|</temp_f>' '{print $2}' KEUG.xml | cut -d. -f1 |grep -v "^$"
88
</snip>
I suppose this is why you should move to an xml parser when you get a chance since that would prevent these little changes from making a difference.
-Ben
More information about the PLUG
mailing list