Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Hilfe shellscript [solved]
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
tazinblack
Veteran
Veteran


Joined: 23 Jan 2005
Posts: 1146
Location: Baden / Germany

PostPosted: Tue Jul 20, 2010 12:20 pm    Post subject: Hilfe shellscript [solved] Reply with quote

Hallo zusammen,

nachdem ich diesem "Phänomen" jetzt schon öfters begegnet bin, frag ich mal Euch im Rat.
Wie kann ich in nem Bash-Skript ne Variable definieren und diese innerhalb von ner while-Schleife ändern?

Beispiel:

Code:
#!/bin/bash

  TEXT=leer

  /usr/bin/df -h | \
  while read LINE
  do
    if [ "${TEXT}" = "leer" ]; then
      TEXT="nicht mehr ${TEXT}"
    fi
    echo "Text : ${TEXT}"
  done

  echo "Text : ${TEXT}"


Das df tut eigentlich nichts zur Sache, ist nur mal eben, um ne Kurze while-Schleife zu haben.
Ich würde gerne innerhalb der while-Schleife den Text entsprechend diversen if-Bedingungen oder ähnlichem anpassen.

Ich hab schon ne Menge versucht im Laufe der Zeit, aber ne gute Lösung hab ich noch nicht gefunden.
Im Prinzip bräuchte ich sowas wie ne globale Variable.
_________________
Gruß / Regards
tazinblack
_______________________________________________________
what's the point in being grown up if you can't be childish sometimes


Last edited by tazinblack on Wed Jul 21, 2010 1:48 pm; edited 1 time in total
Back to top
View user's profile Send private message
firefly
Watchman
Watchman


Joined: 31 Oct 2002
Posts: 5329

PostPosted: Tue Jul 20, 2010 12:39 pm    Post subject: Reply with quote

das liegt daran, dass die shell unter Umständen für die while loop eine subshell erzeugt und somit die TEXT variable am Anfang nicht identisch mit der TEXT variable innerhalb der while schleife ist.
siehe auch: http://fvue.nl/wiki/Bash:_Piped_%60while-read%27_loop_starts_subshell
_________________
Ein Ring, sie zu knechten, sie alle zu finden,
Ins Dunkel zu treiben und ewig zu binden
Im Lande Mordor, wo die Schatten drohn.
Back to top
View user's profile Send private message
Finswimmer
Bodhisattva
Bodhisattva


Joined: 02 Sep 2004
Posts: 5467
Location: Langen (Hessen), Germany

PostPosted: Tue Jul 20, 2010 12:43 pm    Post subject: Reply with quote

Code:

[14:43:50]|[tobi@tobi-desktop]|~
$test=1
[14:44:04]|[tobi@tobi-desktop]|~
$i=1
[14:44:05]|[tobi@tobi-desktop]|~
$while [ $i -le 10 ] ;do  i=$[$i+1] ; test="test2 $(echo $test) $i";echo $test;done
test2 1 2
test2 test2 1 2 3
test2 test2 test2 1 2 3 4
test2 test2 test2 test2 1 2 3 4 5
test2 test2 test2 test2 test2 1 2 3 4 5 6
test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7
test2 test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7 8
test2 test2 test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7 8 9
test2 test2 test2 test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7 8 9 10
test2 test2 test2 test2 test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7 8 9 10 11


Klappt doch ganz gut?
Oder habe ich dich falsch verstanden?

Tobi
_________________
Bitte auf Rechtschreibung, korrekte Formatierung und Höflichkeit achten!
Danke
Back to top
View user's profile Send private message
69719
l33t
l33t


Joined: 20 Sep 2004
Posts: 865

PostPosted: Tue Jul 20, 2010 1:39 pm    Post subject: Reply with quote

firefly hat recht, durch die Subshell, ausgelöst durch die Pipe kannst du nicht an das script zurückgeben. Eine Variante wäre den entsprechenenden Wert in eine Datei zu scheiben und auszulesen.
Back to top
View user's profile Send private message
firefly
Watchman
Watchman


Joined: 31 Oct 2002
Posts: 5329

PostPosted: Tue Jul 20, 2010 4:10 pm    Post subject: Reply with quote

Finswimmer wrote:
Code:

[14:43:50]|[tobi@tobi-desktop]|~
$test=1
[14:44:04]|[tobi@tobi-desktop]|~
$i=1
[14:44:05]|[tobi@tobi-desktop]|~
$while [ $i -le 10 ] ;do  i=$[$i+1] ; test="test2 $(echo $test) $i";echo $test;done
test2 1 2
test2 test2 1 2 3
test2 test2 test2 1 2 3 4
test2 test2 test2 test2 1 2 3 4 5
test2 test2 test2 test2 test2 1 2 3 4 5 6
test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7
test2 test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7 8
test2 test2 test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7 8 9
test2 test2 test2 test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7 8 9 10
test2 test2 test2 test2 test2 test2 test2 test2 test2 test2 1 2 3 4 5 6 7 8 9 10 11


Klappt doch ganz gut?
Oder habe ich dich falsch verstanden?

Tobi


dann probier mal sein beispiel aus ;)
_________________
Ein Ring, sie zu knechten, sie alle zu finden,
Ins Dunkel zu treiben und ewig zu binden
Im Lande Mordor, wo die Schatten drohn.
Back to top
View user's profile Send private message
py-ro
Veteran
Veteran


Joined: 24 Sep 2002
Posts: 1734
Location: Velbert

PostPosted: Tue Jul 20, 2010 5:23 pm    Post subject: Reply with quote

Du musst die Pipe vor dem while loswerden.

Versuch da mal das

Code:
DF=$(df -h)
while read LINE <<<$DF


den die Pipe erzeugt tatsächlihc nee Subshell.

Py
Back to top
View user's profile Send private message
tazinblack
Veteran
Veteran


Joined: 23 Jan 2005
Posts: 1146
Location: Baden / Germany

PostPosted: Wed Jul 21, 2010 6:38 am    Post subject: Re: Hilfe shellscript Reply with quote

tazinblack wrote:
Hallo zusammen,

nachdem ich diesem "Phänomen" jetzt schon öfters begegnet bin, frag ich mal Euch im Rat.
Wie kann ich in nem Bash-Skript ne Variable definieren und diese innerhalb von ner while-Schleife ändern?

Beispiel:

Code:
#!/bin/bash

  TEXT=leer

  /usr/bin/df -h | \
  while read LINE
  do
    if [ "${TEXT}" = "leer" ]; then
      TEXT="nicht mehr ${TEXT}"
    fi
    echo "Text : ${TEXT}"
  done

  echo "Text : ${TEXT}"


Das df tut eigentlich nichts zur Sache, ist nur mal eben, um ne Kurze while-Schleife zu haben.
Ich würde gerne innerhalb der while-Schleife den Text entsprechend diversen if-Bedingungen oder ähnlichem anpassen.

Ich hab schon ne Menge versucht im Laufe der Zeit, aber ne gute Lösung hab ich noch nicht gefunden.
Im Prinzip bräuchte ich sowas wie ne globale Variable.


Sorry, ich hab ja die Ausgabe des Testskripts ganz vergessen :

Code:
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : nicht mehr leer
Text : leer


Aber ich seh schon, ihr habt die richtigen Stichwörter schon genannt.
Ich bastel da nach nochmal dran rum.

Ich wusste ja dass ihr für so Knobelaufgaben zu haben seid :-)
_________________
Gruß / Regards
tazinblack
_______________________________________________________
what's the point in being grown up if you can't be childish sometimes
Back to top
View user's profile Send private message
tazinblack
Veteran
Veteran


Joined: 23 Jan 2005
Posts: 1146
Location: Baden / Germany

PostPosted: Wed Jul 21, 2010 7:13 am    Post subject: Reply with quote

py-ro wrote:
Du musst die Pipe vor dem while loswerden.

Versuch da mal das

Code:
DF=$(df -h)
while read LINE <<<$DF


den die Pipe erzeugt tatsächlihc nee Subshell.

Py


Das gefiel mir am besten, da es schön kompakt ist. Leider geht mit da, wie es aussieht, irgendwie der Zeilenumbruch verloren und ich hab, warum auch immer, ne Endlosschleife. :-(

Ich hab jetzt dem Link entsprechend sowas hier gemacht:
Code:
#!/bin/bash
while read line; do
    foo=bar
    echo foo1: $foo
done < <(/bin/bin/df -h)
echo foo2: $foo

# Example output:
# foo1: bar
# foo2: bar


Das läuft jetzt. Leider ist es damit nicht mehr posix compliant. aber es funktioniert sehr schick, solang man vorher nicht das hier macht:
Code:
set -o posix

_________________
Gruß / Regards
tazinblack
_______________________________________________________
what's the point in being grown up if you can't be childish sometimes
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6780

PostPosted: Wed Jul 21, 2010 7:49 pm    Post subject: Reply with quote

tazinblack wrote:
Leider ist es damit nicht mehr posix compliant.

Wenn es nicht um eine ewig lange Ausgabe geht, kannst Du sie mit POSIX in den Speicher "slurpen":
Code:
#! /bin/sh
while read a
do   echo "Zeile: $a"
done <<EOF
`df`
EOF

Edit: Eine andere Art zu slurpen ist
Code:
#! /bin/sh
IFS='
'
for a in `df`
do   echo "Zeile: $a"
done
Back to top
View user's profile Send private message
69719
l33t
l33t


Joined: 20 Sep 2004
Posts: 865

PostPosted: Thu Jul 22, 2010 8:17 am    Post subject: Reply with quote

mv wrote:
tazinblack wrote:
Leider ist es damit nicht mehr posix compliant.

Wenn es nicht um eine ewig lange Ausgabe geht, kannst Du sie mit POSIX in den Speicher "slurpen":
Code:
#! /bin/sh
while read a
do   echo "Zeile: $a"
done <<EOF
`df`
EOF

Edit: Eine andere Art zu slurpen ist
Code:
#! /bin/sh
IFS='
'
for a in `df`
do   echo "Zeile: $a"
done

Danke für den IFS Tip!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum