Hilfe für Linux-Script gesucht

crazyd

Active member
Themenstarter
Registriert
1 Dez. 2007
Beiträge
1.285
Moin,

ich versuche gerade ein kleines Linux Script zu schreiben. Ziel ist dabei die aktuelle Temperatur aus dem Google Wetter-XML-file auszulesen.
Leider komm ich beim parsen des XML-files nicht klar.

Der Inhal sieht so aus

Code:
<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data="Hanau, Hesse"/><postal_code data="hanau"/><latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2010-04-26"/><current_date_time data="2010-04-26 15:55:00 +0000"/><unit_system data="US"/></forecast_information><current_conditions><condition data="Mostly Cloudy"/><temp_f data="63"/><temp_c data="17"/><humidity data="Humidity: 53%"/><icon data="/ig/images/weather/mostly_cloudy.gif"/><wind_condition data="Wind: NW at 7 mph"/></current_conditions><forecast_conditions><day_of_week data="Mon"/><low data="41"/><high data="62"/><icon data="/ig/images/weather/chance_of_rain.gif"/><condition data="Chance of Rain"/></forecast_conditions><forecast_conditions><day_of_week data="Tue"/><low data="37"/><high data="62"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="Mostly Sunny"/></forecast_conditions><forecast_conditions><day_of_week data="Wed"/><low data="44"/><high data="68"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Clear"/></forecast_conditions><forecast_conditions><day_of_week data="Thu"/><low data="53"/><high data="73"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="Partly Sunny"/></forecast_conditions></weather></xml_api_reply>

Mich interessiert aber nur der Eintrag <temp_c data="17"/> bzw. eigentlich nur die Zahl.

Aber wie komm ich da ran? Wie isoliere ich das am besten und speichere die Zahl in eine Variable?

Wäre echt toll wenn mir jmd einen Tipp geben könnte. Ich hab schon ein wenig mit sed rumprobiert. Es klappt aber irgendwie nicht so richtig.

Viele Grüße
 
Ich benutze das hier in meiner .bashrc (bzw .zshrc):

Code:
weather() { # show weather information for city $1 
  declare -a WEATHERARRAY 
  WEATHERARRAY=( `lynx -dump "http://www.google.com/search?hl=en&lr=&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&q=weather+${1}&btnG=Search" | grep -A 5 -m 1 "Weather for" | grep -v "Add to "`) 
  echo ${WEATHERARRAY[@]} 
}
Damit die Funktion funktioniert (:P) brauchst du lynx.

Was dein Skript angeht, solltest du vielleicht erst mit grep arbeiten und dann per pipe mit sed weiterarbeiten. Geht wahrscheinlich auch komplett mit grep und regular expressions.

Das hier gibt dir z.B. nur die gewünschte Zahl aus:
Code:
grep -o 'temp_c data="[0-9][0-9]"' test.xml | grep -o "[0-9][0-9]"

Wenn du das übers Internet machen willst, kann ich dir lynx empfehlen:
Code:
lynx -dump 192.168.0.2/test.xml | grep -o 'temp_c data="[0-9][0-9]"' test.xml | grep -o "[0-9][0-9]"
 
Danke für die Antwort,

dein Code läuft bei mir nicht, Lynx ist installiert. Es kommt einfach keine Ausgabe.

Ich verzweifel hier noch ;-)

EDIT: ES GEHT!!! JUHU
 
Hast du denn test.xml durch die entsprechende Datei ersetzt? :rolleyes:
Bei mir aufm lokalen Webserver funktioniert's. Müsste also auch bei dir klappen.
 
Schön das es funktioniert. Man muss übrigens nicht unbedingt lynx nehmen auch wget leistet meistens gute Dienste
 
Mit dem -s Parameter von sed kannst du die Ausgabe noch etwas manipulieren:
Code:
lynx -dump 192.168.0.2/test.xml | grep -o 'temp_c data="[0-9][0-9]"' test.xml | grep -o "[0-9][0-9]" | sed 's/[0-9][0-9]/Frankfurt: &°C/g'
Das würde ausgeben: Frankfurt: 17°C

Grüße,
mikar

P.S.: Die halbfertige Funktion:
Code:
weather() { # shows weather information
if [ $# -eq 0 ]; then
echo "weather(): shows information about the weather"
echo "Usage: weather <City>"
fi
local city="$*"
curlonce=$(curl -s "http://www.google.com/ig/api?weather=${city// /+}")
temp=`echo "$curlonce" | grep -o 'temp_c data="[0-9]*"' | grep -o "[0-9]*"`
weather=`echo "$curlonce" | grep -o 'condition data=\"[A-Z].*\"/><temp_f' | grep -o '[A-Z][a-z]*'`
wind=`echo "$curlonce" | grep -o 'wind_condition data=".*mph"' | grep --color=never -o "[A-Z].*mph"`
date=`echo "$curlonce" | grep -o 'current_date_time data=".*000"' | grep --color=never -o '20.*:[0-9]*'`
humidity=`echo "$curlonce" | grep -o 'humidity data=\"Humidity:.*%' | grep --color=never -o '[0-9]*'`
echo Temperature: $temp°C
echo Weather: $weather
echo $wind
echo Humidity: $humidity%
echo Date: $date
}
Ist leider ziemlich schlampig, kanns nicht besser :D. Ich arbeite aber dran.

Ausgabe:
> weather Rio de Janeiro
Temperature: 29°C
Weather: Clear
Wind: N at 7 mph
Humidity: 74%
Date: 2010-04-27 12:00:00
> weather Frankfurt
Temperature: 19°C
Weather: Clear
Wind: N at 7 mph
Humidity: 34%
Date: 2010-04-27 12:55:00
Gut dazu passt z.B. so ein alias:
alias wetter="weather Bad Soden"
 
Danke noch mal für die Hilfe!

Es funktioniert soweit auch super, nur bei einstelligen Temperaturen geht das Script leider nicht. Bin noch am probieren (mit Wildcards etc.), aber bis jetzt will grep noch nicht so wie ich...

EDIT:

Einstellige Ziffern kann ich mittlerweile isolieren, aber mit Vorzeichen (Minusgrade) läuft es noch nicht :-(
 
Danke, ich bin auch eben auf diese Idee gekommen.

Falls es jmd interessiert, mein Code sieht so aus:

Code:
cat weather.xml | grep -o 'temp_c data="'.*'"' | grep -o '[0-9-]*"/><humidity' | grep -o '[0-9-]*'
 
Ich hab heute auch nochmal dran gearbeitet.
Man brauch allerdings libxml und curl, um das Skript nutzen zu können.

Code:
wetterin() { # Wettervorhersage
if [ $# -eq 0 ]; then
echo "Usage: wetterin <Stadt>" ; return 0
fi
local city="$*"
curlde=$(curl -s "http://www.google.de/ig/api?weather=${city// /+}" | iconv - -f latin1 -t utf8)
curlen=$(curl -s "http://www.google.com/ig/api?weather=${city// /+}")
f4day=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[4]/day_of_week' - | grep -o '[A-Z][a-z]'`)
f4cond=(`echo "$curlen" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[4]/condition' - | grep -o '"[A-Z].*[a-z]"' | tr -d '"'`)
f4low=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[4]/low' - | grep -o '[0-9]*'`)
f4high=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[4]/high' - | grep -o '[0-9]*'`)
echo $f4day.:
echo Himmel: $f4cond
echo Niedrigste T.: $f4low°C, Höchste T.: $f4high°C
f3day=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[3]/day_of_week' - | grep -o '[A-Z][a-z]'`)
f3cond=(`echo "$curlen" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[3]/condition' - | grep -o '"[A-Z].*[a-z]"' | tr -d '"'`)
f3low=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[3]/low' - | grep -o '[0-9]*'`)
f3high=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[3]/high' - | grep -o '[0-9]*'`)
echo 
echo $f3day.:
echo Himmel: $f3cond
echo Niedrigste T.: $f3low°C, Höchste T.: $f3high°C
f2day=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[2]/day_of_week' - | grep -o '[A-Z][a-z]'`)
f2cond=(`echo "$curlen" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[2]/condition' - | grep -o '"[A-Z].*[a-z]"' | tr -d '"'`)
f2low=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[2]/low' - | grep -o '[0-9]*'`)
f2high=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[2]/high' - | grep -o '[0-9]*'`)
echo 
echo Übermorgen, $f2day.:
echo Himmel: $f2cond
echo Niedrigste T.: $f2low°C, Höchste T.: $f2high°C
f1day=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[1]/day_of_week' - | grep -o '[A-Z][a-z]'`)
f1cond=(`echo "$curlen" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[1]/condition' - | grep -o '"[A-Z].*[a-z]"' | tr -d '"'`)
f1low=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[1]/low' - | grep -o '[0-9]*'`)
f1high=(`echo "$curlde" | xmllint --xpath '/xml_api_reply/weather/forecast_conditions[1]/high' - | grep -o '[0-9]*'`)
echo 
echo Morgen, $f1day.:
echo Himmel: $f1cond
echo Niedrigste T.: $f1low°C, Höchste T.: $f1high°C
curtemp=(`echo "$curlde" | grep -o 'temp_c data="[-0-9]*"' | grep -o "[-0-9]*"`)
curcond=(`echo "$curlde" | grep -o 'condition data=\"[A-Z].*[a-z]*\"/><temp_f' | grep -o '[A-Z].*[a-z]"' | tr -d '"'`)
curwind=(`echo "$curlde" | grep -o 'wind_condition data=".*km/h"' | grep --color=never -o "[A-Z].*km/h"`)
#curdate=(`echo "$curlde" | grep -o 'current_date_time data=".*000"' | grep --color=never -o '20.*:[0-9]*'`)
curhumi=(`echo "$curlde" | grep -o 'Feuchtigkeit:.*%' | grep -o '[0-9]*'`)
echo 
echo Heute:
echo Temperatur: $curtemp°C
echo Himmel: $curcond
echo $curwind
echo Luftfeuchtigkeit: $curhumi %
#echo Datum: $curdate
}

Die Ausgabe sieht so aus:
> wetterin Frankfurt [~] 21:38:00
Fr.:
Himmel: Chance of Rain
Niedrigste T.: 8°C, Höchste T.: 17°C

Do.:
Himmel: Mostly Sunny
Niedrigste T.: 13°C, Höchste T.: 24°C

Übermorgen, Mi.:
Himmel: Clear
Niedrigste T.: 8°C, Höchste T.: 20°C

Morgen, Di.:
Himmel: Clear
Niedrigste T.: 4°C, Höchste T.: 19°C

Heute:
Temperatur: 16°C
Himmel: Klar
Wind: NO mit Windgeschwindigkeiten von 8 km/h
Luftfeuchtigkeit: 47 %

Grüße,
mikar
 
  • ok1.de
  • ok2.de
  • thinkstore24.de
  • Preiswerte-IT - Gebrauchte Lenovo Notebooks kaufen

Werbung

Zurück
Oben