You are not logged in.
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
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
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
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
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
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
Very kind of you! Thank you!
Offline
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
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
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