Dec 08, 2025 - 06:54 PMDeutsch | English
Hello unlogged user [ Register | Log in ]

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
humbold
15 Post subject: Shellscript Parameter abarbeiten  PostPosted: Sep 15, 2006 - 02:33 PM



Joined: Aug 24, 2005
Posts: 144

Hallo!
Ich suche eine Möglichkeit alle übergebenen Parameter in einer Schleife einzeln abzuarbeiten, und zwar von der Anzahl abhängig. Gibts da eine Möglichkeit? So ähnlich stelle ich mir das vor:

Code:

#!/bin/sh
# Alle Übergebenen Parameter in einer Schleife einzeln abarbeiten

# Anzahl der Parameter ausgeben
echo "Anzahl der Parameter: ${#}"

# Jeden Parameter einzeln ausgeben
counter=1;
pass=${#};
while [ ${counter} -le ${pass} ] ; do
  echo ${${counter}};
  counter=`expr ${counter} + 1`;
done

exit 0;


echo ${${counter}}; soll dabei den jeweiligen Parameter ausgeben. Allerdings kann man das so scheinbar nicht schreiben. Weiß jemand wie man das richtig schreiben kann?

Gruß hum


Last edited by humbold on Sep 16, 2006 - 04:48 PM; edited 2 times in total
 
 View user's profile Send private message  
Reply with quote Back to top
Boregard
Post subject: RE: Shellscript Parameter abarbeiten  PostPosted: Sep 15, 2006 - 02:42 PM



Joined: May 20, 2005
Posts: 323
Location: Niedernberg
Probier mal:
Code:

#!/bin/sh
# Alle Übergebenen Parameter in einer Schleife einzeln abarbeiten

# Anzahl der Parameter ausgeben
echo "Anzahl der Parameter: ${#}"

# Jeden Parameter einzeln ausgeben
counter=1;


pass=${#};

while [ ${counter} -le ${pass} ] ; do
  echo ${counter};
  let "counter=${counter} + 1";
done

exit 0;
 
 View user's profile Send private message  
Reply with quote Back to top
Hack-o-Master
Post subject:   PostPosted: Sep 15, 2006 - 02:47 PM



Joined: Apr 05, 2005
Posts: 499
Location: Bielefeld <:-P
Oder so:
Code:
#!/bin/sh
# test.sh : Testskript Parameterauswertung

counter=1

while [ "$1" ]; do
  echo "$counter. $1"
  ((counter++))
  shift # nächsten Parameter nachschieben
done
Aufruf:
Code:
sh test.sh aber wieso muss das denn sein "leer  zeichen muss geqouted werden"
ergibt dann:
Code:
1. aber
2. wieso
3. muss
4. das
5. denn
6. sein
7. leer  zeichen muss geqouted werden
bzw. ohne die schönere Bildschirmausgabe:
Code:
#!/bin/sh
# test.sh : Testskript Parameterauswertung

while [ "$1" ]; do
  echo "$1" # Mach was mit $1
  shift # nächsten Parameter nachschieben
done

mfg. Hack-o-Master

_________________
sidux2007-02 64Bit, Kernel 2.6.23rc..(Crypto-root), X.org 7.3, KDE 3.5.7
http://bernd-das-brot.de.be | Gästebuch , certified B.O.F.H.
 
 View user's profile Send private message Send e-mail Visit poster's website  
Reply with quote Back to top
humbold
Post subject:   PostPosted: Sep 15, 2006 - 03:46 PM



Joined: Aug 24, 2005
Posts: 144

@Boregard
Das funktioniert leider nicht. echo ${counter} gibt nur den Wert von counter aus, aber nicht den Parameter Nr. counter. Aber den let kannte ich noch nicht.

@Hack-o-Master
Ja, genau sowas meinte ich! Allerdings sind die Parameter danach alle weggeshiftet. Also muss man die dann schon sichern wenn man sie später noch braucht. Leider weiß man das ja vorher nicht wieviele übergeben werden. Gibts da sowas wie arrays die zur Laufzeit erweitert werden können? Wüsste sonst nicht wie man das machen sollte, denn man weiß ja vorher nicht wieviele Parameter übergeben werden.
Wäre schon gut zu wissen wie man das machen kann. Funtioniert für mein aktuelles Problem aber auch so.

Besten Dank auf jeden Fall für die schnellen Antworten! Smilie

Gruß hum
 
 View user's profile Send private message  
Reply with quote Back to top
Hack-o-Master
Post subject:   PostPosted: Sep 15, 2006 - 04:22 PM



Joined: Apr 05, 2005
Posts: 499
Location: Bielefeld <:-P
@humbold

Mit Speichern der Parameter im Array PARAM:
Code:
#!/bin/sh
# test4.sh : Testskript Parameterauswertung

declare -a PARAM        # Array mit den einzelen gespeicherten Parametern

echo "Originale Parameter:"
counter=0
while [ "$1" ]; do
  ((counter++))
  echo "$counter. $1"
  PARAM[$counter]="$1"  # Parameter abspeichern
  shift # nächsten Parameter nachschieben
done
maxparams=$counter

# Gespeicherte Parameter ausgeben
echo "Gespeicherte Parameter:"

for counter in $(seq 1 $maxparams)
do
  echo "$counter. ${PARAM[$counter]}"
done
mfg. Hack-o-Master

_________________
sidux2007-02 64Bit, Kernel 2.6.23rc..(Crypto-root), X.org 7.3, KDE 3.5.7
http://bernd-das-brot.de.be | Gästebuch , certified B.O.F.H.
 
 View user's profile Send private message Send e-mail Visit poster's website  
Reply with quote Back to top
humbold
15 Post subject: arrays  PostPosted: Sep 16, 2006 - 04:46 PM



Joined: Aug 24, 2005
Posts: 144

Besten Dank Hack-o-Master! Brauch das jetzt auch in meinem script! Smilie

Hab auch noch ne gute Seite zum Thema arrays gefunden:
http://db.ilug-bom.org.in/Documentation/abs-guide/arrays.html

Gruß hum
 
 View user's profile Send private message  
Reply with quote Back to top
humbold
Post subject:   PostPosted: Sep 18, 2006 - 05:56 PM



Joined: Aug 24, 2005
Posts: 144

@Hack-o-Master
Code grad erst weiter. Find Lösung mit dem "seq" in der for-Schleife is echt schön! Smilie

Gruß hum
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2007 The PNphpBB Group
Credits
 
Deutsch | English
Logos and trademarks are the property of their respective owners, comments are property of their posters, the rest is © 2004 - 2006 by Jörg Schirottke (Kano).
Consult Impressum and Legal Terms for details. Kanotix is Free Software released under the GNU/GPL license.
This CMS is powered by PostNuke, all themes used at this site are released under the GNU/GPL license. designed and hosted by w3you. Our web server is running on Kanotix64-2006.