Homework 03: weather.sh
This commit is contained in:
parent
8e5dd36491
commit
9d06c32de9
1 changed files with 60 additions and 0 deletions
60
homework03/weather.sh
Executable file
60
homework03/weather.sh
Executable file
|
|
@ -0,0 +1,60 @@
|
|||
#!/bin/sh
|
||||
|
||||
# Globals
|
||||
|
||||
URL="https://forecast.weather.gov/zipcity.php"
|
||||
ZIPCODE=46556
|
||||
FORECAST=0
|
||||
CELSIUS=0
|
||||
|
||||
# Functions
|
||||
|
||||
usage() {
|
||||
cat 1>&2 <<EOF
|
||||
Usage: $(basename $0) [zipcode]
|
||||
|
||||
-c Use Celsius degrees instead of Fahrenheit for temperature
|
||||
-f Display forecast text after the temperature
|
||||
|
||||
If zipcode is not provided, then it defaults to $ZIPCODE.
|
||||
EOF
|
||||
exit $1
|
||||
}
|
||||
|
||||
weather_information() {
|
||||
# Fetch weather information from URL based on ZIPCODE
|
||||
curl -sL "$URL?inputstring=$ZIPCODE"
|
||||
}
|
||||
|
||||
temperature() {
|
||||
# Extract temperature information from weather source
|
||||
if [ $CELSIUS -eq 0 ]; then
|
||||
weather_information | grep -Eo '\-?[0-9]{1,3}°F<' | sed 's/°F<//'
|
||||
else
|
||||
weather_information | grep -Eo '\-?[0-9]{1,3}°C<' | sed 's/°C<//'
|
||||
fi
|
||||
}
|
||||
|
||||
forecast() {
|
||||
# Extract forecast information from weather source
|
||||
weather_information | grep -Eo '"myforecast-current">.*?<' | sed 's/"myforecast-current">//g; s/<//g; s/^[[:space:]]*//; s/[[:space:]]*$//'
|
||||
}
|
||||
|
||||
# Parse Command Line Options
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
-c) CELSIUS=1;;
|
||||
-f) FORECAST=1;;
|
||||
-h) usage 0;;
|
||||
*) ZIPCODE=$1;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
# Display Information
|
||||
|
||||
echo "Temperature: $(temperature) degrees"
|
||||
if [ $FORECAST -eq 1 ]; then
|
||||
echo "Forecast: $(forecast)"
|
||||
fi
|
||||
Loading…
Reference in a new issue