You are not logged in.

#1 2021-09-12 05:31:13

leodova
Member
Registered: 2021-09-07
Posts: 14

[SOLVED] Is what I want to do possible?

I have set up a scheduled job to run a weather command that gets the current weather in my area.  It lists the temp, humidity etc. The last line of the output is if there are any active weather alerts in the area. If there are no alerts it says:

(no current alerts for this zone)

I want it to put the weather information in an email and mail it to me only if there are some weather alerts. Basically, if it says the above line don't email me, if anything else is in that line...email me.

I hope I am clear. But I suspect this would need some kind of programming language and I'm totally illiterate in that regard.

Last edited by leodova (2021-09-12 09:01:21)

Offline

#2 2021-09-12 08:22:17

damo
....moderator....
Registered: 2015-08-20
Posts: 6,719

Re: [SOLVED] Is what I want to do possible?

I would put the weather info into a variable (or text file?), to be emailed when required.

Presumably your code uses an if statement to display "(no current alerts for this zone)", so then you could do

if <whatever> then
    echo "(no current alerts for this zone)"
else
   email the info
fi

Edit--- I just noticed that you said you haven't done any coding hmm

Last edited by damo (2021-09-12 08:24:11)


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#3 2021-09-12 08:25:19

damo
....moderator....
Registered: 2015-08-20
Posts: 6,719

Re: [SOLVED] Is what I want to do possible?

What is the "weather command" you are using?


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#4 2021-09-12 08:32:22

leodova
Member
Registered: 2021-09-07
Posts: 14

Re: [SOLVED] Is what I want to do possible?

damo wrote:

What is the "weather command" you are using?

It is just the weather-util command.

I've figured out how to email it, but I only want it to email me when it doesn't say no alerts, basically when there is an alert present in the output.

But there's no way to predict what text will be in an alert.

Last edited by leodova (2021-09-12 08:34:43)

Offline

#5 2021-09-12 08:51:46

twoion
一期一会
Registered: 2015-08-10
Posts: 3,357

Re: [SOLVED] Is what I want to do possible?

Complete idea:

#!/usr/bin/env bash

set -o pipefail

WEATHER_COMMAND=/usr/bin/weather-util # change if required
TEMP=$(mktemp) # create a named temporary file

if ! "$WEATHER_COMMAND" >"$TEMP"; then
  # if the weather command failed, we can't do anything.
  exit 1
fi

if tail -n 1 "$TEMP" |  grep -q '^(no current alerts for this zone)$'; then
  # the last line says it doesn't contain any alerts!
 exit 0
else
  # here you send your email, for example the output of the weather-util command
  cat "$TEMP" | msmtp ...
  exit 0
fi

Save this script in ~/.local/bin, add the executable bit with chmod +x and run it from your cronjob, timer etc.

Offline

#6 2021-09-12 09:00:19

leodova
Member
Registered: 2021-09-07
Posts: 14

Re: [SOLVED] Is what I want to do possible?

Very kind of you! Thank you!

Offline

#7 2021-09-12 10:25:59

leodova
Member
Registered: 2021-09-07
Posts: 14

Re: [SOLVED] Is what I want to do possible?

twoion wrote:

Complete idea:

Save this script in ~/.local/bin, add the executable bit with chmod +x and run it from your cronjob, timer etc.

Sorry to bug you. So if I change this line:

WEATHER_COMMAND=/usr/bin/weather-util # change if required

to

WEATHER_COMMAND=$(weather-util fips16057) # change if required

FIPS being the code needed to add to the command in order to display weather info for my zone, it simply outputs the weather info to the terminal with "No such file or directory" added to the end of it for some reason.

However, if I leave it alone and simply run it with no options, it mails me the list of options, which is what I would expect because that's the default output if you don't give it an option.

I hope I am clear and explaining myself clearly!!

Last edited by leodova (2021-09-12 10:26:58)

Offline

#8 2021-09-12 10:39:54

twoion
一期一会
Registered: 2015-08-10
Posts: 3,357

Re: [SOLVED] Is what I want to do possible?

If you want to do it like that and with $() subshells, you can do:

set -o pipefail
CAPTURE=$(weather-util fips16057 2>/dev/null)
if (( $? != 0 )); then
  # command failed, doesn't make sense to proceed.
  exit 1
fi
if printf "%s\n" "$CAPTURE" | tail -n 1 | grep -q '^(no current alerts for this zone)$'; then
  # the last line says it doesn't contain any alerts, do nothing
  true
else
  # here you send your email, for example the output of the weather-util command
  printf "%s\n" "$CAPTURE" | msmtp ...
fi
exit 0

This does not need temporary files anymore, as the output of the weather util is stored in the CAPTURE variable.

Offline

#9 2021-09-12 10:54:12

leodova
Member
Registered: 2021-09-07
Posts: 14

Re: [SOLVED] Is what I want to do possible?

So cool! Works perfectly now.

Something I could never get any weather app to do correctly, simply send me a weather alert when there was one for my area! Now it's accomplished and no third party app on my phone looking at who knows what!

Offline

Board footer

Powered by FluxBB