159 lines
3.7 KiB
TeX
Executable File
159 lines
3.7 KiB
TeX
Executable File
\section{For Schleife}
|
|
\begin{flushleft}
|
|
|
|
Die {\ttfamily for} Schleife erh"alt eine Liste von Werten und f"uhrt die Kommandos mit jedem
|
|
Wert aus. M"ochte man eine Schleife abbrechen, so wird hierzu der Befehl {\ttfamily break [n]}
|
|
benutzt. Um wieder am Anfang zu springen, benutz man den Befehl {\ttfamily continue [n]}.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Beispiel For Schleife]
|
|
uws@tux>cat example.sh
|
|
#!/bin/env bash
|
|
for a in $1
|
|
do
|
|
printf "\n\tUebergabe Wert: ${a}\n"
|
|
done
|
|
\end{lstlisting}
|
|
Hier ein Beispiel f"ur {\ttfamily break / continue}.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Beispiel Break / Continue]
|
|
uws@tux>cat example1.sh
|
|
#!/bin/env bash
|
|
for a in 1 2 3 4 5
|
|
do
|
|
if [ ${a} -eq 2 ]
|
|
then
|
|
continue
|
|
fi
|
|
if [ ${a} -eq 4 ]
|
|
then
|
|
break
|
|
fi
|
|
printf "\n\tWerte: ${a}\n"
|
|
done
|
|
\end{lstlisting}
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Example2.sh]
|
|
uws@tux>cat example2.sh
|
|
#!/bin/env bash
|
|
i=1
|
|
wochentage="Montag Dienstag Mittwoch Donnerstag Freitag"
|
|
for tage in ${wochentage}
|
|
do
|
|
printf "\n\tWochentag $((i++)) : ${tage}\n"
|
|
done
|
|
|
|
uws@tux>./example2.sh
|
|
Wochentag 1 : Montag
|
|
Wochentag 2 : Dienstag
|
|
Wochentag 3 : Mittwoch
|
|
Wochentag 4 : Donnerstag
|
|
Wochentag 5 : Freitag
|
|
\end{lstlisting}
|
|
\newpage
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Example3.sh]
|
|
uws@tux>cat example3.sh
|
|
#!/bin/env bash
|
|
i=1
|
|
cd ~/bin
|
|
for item in *
|
|
do
|
|
printf "\n\tItem $((i++)) : ${item}\n"
|
|
done
|
|
|
|
uws@tux>./example3.sh
|
|
Item 1 : example.sh
|
|
Item 2 : example1.sh
|
|
Item 3 : example2.sh
|
|
Item 4 : example3.sh
|
|
\end{lstlisting}
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Example4.sh]
|
|
uws@tux>cat example4.sh
|
|
#!/bin/env bash
|
|
i=1
|
|
for file in /etc/[abcd]*.conf
|
|
do
|
|
printf "\n\tFile $((i++)) : ${file}\n"
|
|
done
|
|
|
|
uws@tux>./example4.sh
|
|
File 1 : /etc/alsa-pulse.conf
|
|
File 2 : /etc/asound-pulse.conf
|
|
File 3 : /etc/blkid.conf
|
|
File 4 : /etc/dhcpclient.conf
|
|
\end{lstlisting}
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Example5.sh]
|
|
uws@tux>cat example5.sh
|
|
#!/bin/env bash
|
|
i=1
|
|
for file in $(ls ~/bin/*.sh)
|
|
do
|
|
printf "\n\tFile $((i++)) : ${file}\n"
|
|
done
|
|
|
|
uws@tux>./example5.sh
|
|
File 1 : /home/uws/bin/example.sh
|
|
File 2 : /home/uws/bin/example1.sh
|
|
File 3 : /home/uws/bin/example2.sh
|
|
File 4 : /home/uws/bin/example3.sh
|
|
File 5 : /home/uws/bin/example4.sh
|
|
File 6 : /home/uws/bin/example5.sh
|
|
\end{lstlisting}
|
|
Beispiel einer {\ttfamily for} Schleife in einer Zeile.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=Eine Zeile]
|
|
uws@tux>for a in 01 02 03; do ls bild${a}.jpg; done
|
|
bild01.jpg
|
|
bild02.jpg
|
|
bild03.jpg
|
|
\end{lstlisting}
|
|
\newpage
|
|
In dem n"achsten Beispiel wird automatisch hochgez"ahlt und in Schritten.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=example6.sh]
|
|
uws@tux>cat example6.sh
|
|
#!/bin/env bash
|
|
for ((i=1;i<=5;i++)); do
|
|
echo "$i: "
|
|
done
|
|
|
|
#von 1 bis 10
|
|
for i in {1..10}
|
|
do
|
|
printf "Number: $1\n"
|
|
done
|
|
|
|
#von 1 bis 10, aber in 2er Schritten
|
|
for i in {1..10..2}
|
|
do
|
|
printf "Number: $i\n"
|
|
done
|
|
\end{lstlisting}
|
|
Nun wird eine Datei eingelesen und nur einzelne Spalten werden ausgegeben.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=example7.sh]
|
|
uws@tux>cat example7.sh
|
|
for SHARE in $(grep -i "nfs" /etc/fstab | cut -d' ' -f2)
|
|
do
|
|
printf "\nShare to mount: $SHARE"
|
|
done
|
|
\end{lstlisting}
|
|
Die Ausgabe erfolgt mittels einer Array-Variable.
|
|
\listBash
|
|
\begin{lstlisting}[captionpos=b, caption=example8]
|
|
uws@tux>declare -a MyUser=('uws' 'paul' 'Hans')
|
|
|
|
uws@tux>for ((i=0;i<=2;i++));do echo User: ${MyUser[$i]};done
|
|
User: uws
|
|
User: paul
|
|
User: Hans
|
|
|
|
uws@tux>for i in ${!MyUser[*]};do printf "User: ${MyUser[$i]};done
|
|
User: uws
|
|
User: paul
|
|
User: Hans
|
|
\end{lstlisting}
|
|
\end{flushleft}
|