Probleme mit Variablen in BASH-Scripts
Das folgende BASH-Script ruft eine Oracle-Abfrage auf und liefert in $LINE entweder 0 oder einen Wert > 0. Wenn der Wert 0 ist soll das Script beendet werden. 1 ANZAHL=0 2 sqlplus -s $EMAILLOGIN v_status.sql | while read LINE 3 do 4 if (test $LINE ) then 5 ANZAHL=`expr $LINE` 6 fi 7 done 8 if ( test $ANZAHL -eq 0 ) then 9 exit 0 10 fi In Zeile 5 wird ANZAHL auf den Wert in $LINE gesetzt (z.B. 5). Das Funktioniert. Beim Vergleich in Zeile 8 hat ANZAHL jedoch wieder den Initialwertwert von Zeile 1. Welche Möglichkeiten habe ich, um ANZAHL global zu benutzen ? oder wie kann man das Problem anders lösen ? Gruß Stefan Wyrwa -- Um aus der Liste ausgetragen zu werden, eine Mail an majordomo@suse.com schicken, mit dem Text: unsubscribe suse-linux
Am Fre, 08 Jan 1999 schrieb system: Hallo System,
Das folgende BASH-Script ruft eine Oracle-Abfrage auf und liefert in $LINE entweder 0 oder einen Wert > 0.
Ich sehe das richtig? In LINE steht nur eine Zahl?
Wenn der Wert 0 ist soll das Script beendet werden.
1 ANZAHL=0 2 sqlplus -s $EMAILLOGIN v_status.sql | while read LINE
Ob Zeile 2 richtig ist weiß ich nicht. read als while-Bedinung ist merkwürdig, da es niemals fehlschlägt. Wenn das so sein soll, so ist doch while true und read nach dem do besser.
3 do 4 if (test $LINE ) then
hier gehören keine runde Klammern hin. Es wird gefragt, ob LINE nicht leer ist oder nicht. Möchtest Du eine Abfrage nach 0, so heißt das: if test $LINE -gt 0. Möchtest Du tatsächlich abfragen, ob LINE leer ist, so wäre if test -n $LINE besser,siehe help test. Auf jeden Fall gehöhrt then in die nächste Zeile. Oder ein Semikolon davor, was aber nicht so schön ist.
5 ANZAHL=`expr $LINE`
Was soll das Bezwecken? Wenn ANZAHL den Wert von LINE übernehmen soll, so reicht ANZAHL=$LINE. Aber warum nicht mit LINE weiterarbeiten. Was expr so alles kann siehe man expr.
6 fi 7 done
Nach dem done kommt die Shell nie mehr hin, da while immer true ist.
8 if ( test $ANZAHL -eq 0 ) then
Die runden Klammern sind überflüssig.
9 exit 0 10 fi
In Zeile 5 wird ANZAHL auf den Wert in $LINE gesetzt (z.B. 5). Das Funktioniert.
aber völlig kompliziert und nicht gut. s.o
Beim Vergleich in Zeile 8 hat ANZAHL jedoch wieder den Initialwertwert von Zeile 1.
Woher weißt Du das? So wie das Skript da steht kommt es niemals zu Zeile 8.
Welche Möglichkeiten habe ich, um ANZAHL global zu benutzen ?
export ANZAHL, ist hier aber unnötig.
oder wie kann man das Problem anders lösen ?
An der Bash-Programmierung muß Du noch einiges tun. Am Besten Du besorgst Dir ein Buch dazu. Ansonsten man bash info bash help bashbefehl und im Linuxanwenderhandbuch auf /usr/doc/Books steht schon eine ganze Menge. Das dürfte auch fürs erste reichen. Da brauchst Du noch nicht einmal Geld auszulegen. Aber keine Angst, daß machst Du schon. cu Bernd -- Bernd Brodeßer Mönchengladbach B.Brodesser@online-club.de -- Um aus der Liste ausgetragen zu werden, eine Mail an majordomo@suse.com schicken, mit dem Text: unsubscribe suse-linux
On Fri, 8 Jan 1999, Bernd Brodesser wrote:
Am Fre, 08 Jan 1999 schrieb system:
2 sqlplus -s $EMAILLOGIN v_status.sql | while read LINE
Ob Zeile 2 richtig ist weiß ich nicht. read als while-Bedinung ist merkwürdig, da es niemals fehlschlägt. Wenn das so sein soll, so ist doch while true und read nach dem do besser.
Das kann so nicht sein, das Konstrukt ist durchaus ueblich. Der Test mit "read HLP; echo $?" ergibt bei Eingabe von EOF (^D) auch 1, also false. Die Struktur ist im Prinzip OK, aber ein paar Konstrukte sind suspekt und die Redundanzen koennen raus. Was bei Scripten ganz gut funktioniert, ist Abgucken. Ein laufendes System ist voll davon. Gerhard Sittig -- If you don't understand or are scared by any of the above ask your parents or an adult to help you. -- Um aus der Liste ausgetragen zu werden, eine Mail an majordomo@suse.com schicken, mit dem Text: unsubscribe suse-linux
On Fri, 8 Jan 1999, system wrote:
Welche Möglichkeiten habe ich, um ANZAHL global zu benutzen ?
"man bash", Stichworte "declare" und "local" Offenbar gibt es ZWEI Variablen (eine aeussere und die innere). Die innere ist lokal und "verdeckt" die aeussere. Gerhard Sittig -- If you don't understand or are scared by any of the above ask your parents or an adult to help you. -- Um aus der Liste ausgetragen zu werden, eine Mail an majordomo@suse.com schicken, mit dem Text: unsubscribe suse-linux
system wrote:
Das folgende BASH-Script ruft eine Oracle-Abfrage auf und liefert in $LINE entweder 0 oder einen Wert > 0. Wenn der Wert 0 ist soll das Script beendet werden.
1 ANZAHL=0 2 sqlplus -s $EMAILLOGIN v_status.sql | while read LINE 3 do 4 if (test $LINE ) then 5 ANZAHL=`expr $LINE` 6 fi 7 done 8 if ( test $ANZAHL -eq 0 ) then 9 exit 0 10 fi
In Zeile 5 wird ANZAHL auf den Wert in $LINE gesetzt (z.B. 5). Das Funktioniert. Beim Vergleich in Zeile 8 hat ANZAHL jedoch wieder den Initialwertwert von Zeile 1.
Welche Möglichkeiten habe ich, um ANZAHL global zu benutzen ? oder wie kann man das Problem anders lösen ?
Hi ?system?, versuch mal ....... declare -x ANZAHL ANZAHL=0 ........... Zu finden auch in (ich_trauŽs_mich_fast_nicht_zu_sagen) : man bash :-) o long... bernd -- Please try to boot your system outside the system, it maybe the grounding problem. -- Um aus der Liste ausgetragen zu werden, eine Mail an majordomo@suse.com schicken, mit dem Text: unsubscribe suse-linux
participants (4)
-
B.Brodesser@online-club.de
-
G.Sittig@abo.FreiePresse.DE
-
Illuminatus@t-online.de
-
system@automobile.blg.de