Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
In bash Variablen auslesen
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
Ampheus
l33t
l33t


Joined: 19 Oct 2005
Posts: 814
Location: Rheine, NRW, Germany

PostPosted: Tue May 22, 2007 12:50 pm    Post subject: In bash Variablen auslesen Reply with quote

Hi, ich habe mir ein kleines bash-script gebastelt und möchte nun variablen aus einer Datei auslesen. Der Syntax der Datei ist wie folgt:
Code:
network={
        name=""
        interface=""
        ip="foo"
        broadcast=""
        netmask=""
        dns=""
}

network={
        name=""
        interface=""
        ip="bar"
        broadcast=""
        netmask=""
        dns=""
}

Als erstes sollten die Netzwerke gezählt werden und dann hinterher die Einträge an Variablen übergeben wären, also z.B.
Code:
IP[0]="foo"
IP[1]="bar"


Wie könnte ich das am Besten realisiern? Sollte ich mit sed arbeiten oder doch ganz anders?
Back to top
View user's profile Send private message
Fabiolla
Apprentice
Apprentice


Joined: 04 Mar 2004
Posts: 277
Location: somewhere

PostPosted: Tue May 22, 2007 3:26 pm    Post subject: Reply with quote

Hallo,
Bash-Arrays wären eine Möglichkeit.

1. IP-Adressen in ein Array einlesen:
Code:

IPARRAY=($(grep "ip=" inputfile | cut -d "\"" -f 2))


2. Die Werte im Array ausgeben/einer anderen Variablen zuweisen:
Code:

for VALUE in ${IPARRAY[@]}; do echo $VALUE; done


Die Anzahl von ip= könnte beim grep z.B mit wc -l ermittelt werden.

lg
Back to top
View user's profile Send private message
Ampheus
l33t
l33t


Joined: 19 Oct 2005
Posts: 814
Location: Rheine, NRW, Germany

PostPosted: Tue May 22, 2007 8:30 pm    Post subject: Reply with quote

Dein Tipp brachte mich schon um einiges weiter, danke. Allerdings habe ich immernoch das Problem, dass ich die ausgelesenen strings nicht ausgeben möchte, sondern in ein array speichern möchte, also in diesem fall in IP[].
Es soll also automatisch hochgezählt werden. Das Ergebnis wäre, dass IP[0]=foo und IP[1]=bar wäre.
Back to top
View user's profile Send private message
Fabiolla
Apprentice
Apprentice


Joined: 04 Mar 2004
Posts: 277
Location: somewhere

PostPosted: Wed May 23, 2007 10:49 am    Post subject: Reply with quote

Hallo,
das Array hättest du ja schon mit:
Code:

IPARRAY=($(grep "ip=" inputfile | cut -d "\"" -f 2))


Eine andere Form der Ausgabe des Arrays:
Code:

# Anzahl der Array-Elemente ermitteln
UBOUND=${#IPARRAY[*]}

# Die Array-Elemente von 0 beginnend fortlaufend ausgeben
for (( I=0; $I < $UBOUND; I++ )); do echo ${IPARRAY[$I]}; done

# oder Ausgabe in Form IP[0]=, IP[1]=
for (( I=0; $I < $UBOUND; I++ )); do echo "IP[$I]="${IPARRAY[$I]}; done


lg
Back to top
View user's profile Send private message
Ampheus
l33t
l33t


Joined: 19 Oct 2005
Posts: 814
Location: Rheine, NRW, Germany

PostPosted: Wed May 23, 2007 12:31 pm    Post subject: Reply with quote

Danke sehr! Du hast mir sehr geholfen :) Und ich dachte schon, ich müsste mich mit sed auseinandersetzen :)
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