\input{configpres} \title{Einführung in die Shell-Programmierung} \maketitle \begin{frame} \frametitle{Aufbau von Shell-Skripten} \begin{itemize} \item Syntax: "\#!/ein/interpreter" \pause \item Beliebiges Programm als Interpreter möglich \pause \item Beispiele hier: "\#!/bin/sh" \pause \item Danach beliebige Shell-Befehle möglich \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Hello World als Shell-Script} \begin{lstlisting} #!/bin/sh echo Hello World \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Variablen} \begin{lstlisting} #!/bin/sh # VAR1 defined for this process VAR1=abc # now VAR1 is defined for children processes export VAR1 # VAR2 defined only for /bin/sh child process # (with export set) VAR2=def /bin/sh \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Variablen} \begin{lstlisting} #!/bin/sh VAR1=abc echo $VAR1 echo ${VAR1} \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Variablen und Parameter} \begin{lstlisting} #!/bin/sh MY_VAR=17 PAR_1=$1 datum=`date` echo variable is ${MYVAR}, parameter is ${PAR_1} echo date is $datum echo hostname of this machine is $(hostname) echo \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Ausgabe-Umleitung} \begin{lstlisting} #!/bin/sh # How many lines in addr.txt contain "Jones" ? # Store the result in "jones_count" grep Jones addr.txt | wc -l > jones_count # Append a message to a log file echo "My message" >> log_file exit 0 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Bedingte Verzweigungen} \begin{lstlisting} #!/bin/sh if [ bedingung ] then ... else ... fi # in one line: if [ bedingung ] ; then ... ; fi \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Bedingte Verzweigungen} \begin{lstlisting} #!/bin/sh # Zahlentests: -eq -ne -lt -gt -le -ge zahl=15 if [ $zahl -lt 20 ] ; then ... # String-Tests: = != < > -n -z string="Hallo" if [ $string != "hello" ] ; then ... \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Bedingte Verzweigungen} \begin{lstlisting} #!/bin/sh # Dateitests, z.B. if [ -f $datei ] ; then ... # Auswahl: # -f ist eine normale Datei # -d ist ein Verzeichnis # -e existiert # -s existiert und ist nicht leer # -r ist lesbar # -w ist schreibbar # -x ist ausfuehrbar # -b ist ein Block Device # -c ist ein Character Device \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Bedingte Verzweigungen} \begin{lstlisting} #!/bin/sh # Does addr.txt contain "Jones" ? if grep Jones addr.txt ; then ... # Return value 0 means OK exit 0 \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Bedingte Verzweigungen} \begin{lstlisting} #!/bin/sh VAR=abcdef case $VAR in abc) echo "exactly abc" ;; abcdef) echo "exactly abcdef" ;; abc*) echo "begins with abc" ;; *) echo "match everything" ;; esac \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Bedingte Verzweigungen} \begin{lstlisting} #!/bin/sh # Short circuit tests: make && make install grep Jones addr.txt || echo "No Jones found!" \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Schleifen} \begin{lstlisting} #!/bin/sh for datei in datei1 datei2 datei3 do cp $datei $datei.bak done for datei in * do ... done \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Schleifen} \begin{lstlisting} #!/bin/sh while [ bedingung ] do ... done while [ bedingung ] ; do ... ; done # bedingung wie bei if \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Funktionen} \begin{lstlisting} #!/bin/sh error_log() { echo "Fehler: $1" >> my_log_file } ... error_log "Datei nicht gefunden." \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Einbinden anderer Skripte} \begin{lstlisting} #!/bin/sh # Include an external script: . my_other_script # run another script/command in the background: my_other_script& \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Zugriff auf Dateien} \begin{lstlisting} #!/bin/sh ls -1 > dateiliste.txt while read dateiname do if [ -d $dateiname ] then echo "$dateiname ist ein Verzeichnis" fi done < dateiliste.txt \end{lstlisting} \end{frame} \input{tailpres}