<!doctype html> <html lang="de"> <meta charset="utf-8"> <title>Einführung in Linux</title> <meta name="description" content="YALC - Yet Another Linux Course "> <meta name="author" content="Daniel Schubert"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" href="css/reveal.css"> <link rel="stylesheet" href="css/theme/league.css" id="theme"> <link rel="icon" href="img/openlogo-nd-25.png" type="img/png"> <!-- Theme used for syntax highlighting of code --> <link rel="stylesheet" href="lib/css/zenburn.css"> <!-- Printing and PDF exports --> <script> var link = document.createElement( 'link' ); link.rel = 'stylesheet'; link.type = 'text/css'; link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css'; document.getElementsByTagName( 'head' )[0].appendChild( link ); </script> </head> <body> <div class="reveal"> <!-- Any section element inside of this container is displayed as a slide --> <div class="slides"> <section data-transition="slide" data-background="#4d7e65" data-background-transition="zoom"> <h1>LINUX</h1> <h3>Das universelle Betriebssystem</h3> <p>Eine Einführung - Teil 5 <p> <small>Deutsche Angestellten Akademie</small> </p> <aside class="notes"> Frage: Fragen zur letzten Stunde? <h3>LINKS</h3> <p>http://tldp.org/LDP/abs/html/complexfunct.html <p>http://programmingexamples.wikidot.com/bash-scripting#toc24 <div> - script kontrolle ( strg-z etc ) - input - parameter - functions - kontrollstrukturen - </aside> </section> <section> <h2>Bash scripting - Fortsetzung</h2> <h3>Wiederholung</h3> </section> <section> <h3>Variablen</h3> <pre><code>#!/bin/bash x=10 #NICHT x = 10 -> keine Leerzeichen! X=20 #variablen sind Case Sensitive $y= #NULL variable echo "x = $x" echo "X = $X" echo "y = $y" unset x #variable löschen echo $x</code></pre> </section> <section> <h3>Kommentare</h3> <pre><code> #!/bin/bash # Diese Zeile ist ein Kommentar echo "A comment will follow." # Comment here. echo "The # here does not begin a comment." echo 'The # here does not begin a comment.' echo The \# here does not begin a comment. echo The # here begins a comment. echo ${PATH#*:} # Parameter substitution, not a comment. echo $(( 2#101011 )) # Base conversion, not a comment. </code> </pre> </section> <section> <h3>Quoting</h3> <ul style="font-size: .8em "> <li class="fragment "><code>" "</code> (Anführungszeichen) Alles zwischen diesen Zeichen ist buchstabengetreu zu interpretieren. Ausnahmen :<code> $ ` " \ </code></li> <li class="fragment "><code>' ' </code>(Ticks) Alles zwischen diesen Zeichen wird wörtlich genommen, mit Ausnahme eines weiteren <code>'</code>.</li> <li class="fragment "><code>\</code> (Backslash) Das Zeichen nach einem <code>\</code> wird wörtlich genommen. Anwendung z. B. innerhalb von " ", um ", $ und ` zu entwerten.</li> <li class="fragment "><code>` `</code> (Backticks) Alles innerhalb wird als Kommando interpretiert.</li> </ul> </section> <section> <pre><code>$ echo 'Ticks "schützen" Anführungszeichen' Ticks "schützen" Anführungszeichen $ echo "Ist dies ein \"Sonderfall\"?" Ist dies ein "Sonderfall"? $ echo "Sie haben `ls | wc -l` Dateien in `pwd`" Sie haben 43 Dateien in /home/dany $ echo "Der Wert von \$x ist $x" Der Wert von $x ist 100 # Maskierung der Leerstelle mit \ message="hallo Welt" message=hello\ Welt </code></pre> </section> <section><pre style="font-size: .5em;width: auto"> echo 'So sprach'"'"'s und ging ohne einen Backslash (\) weiter.' └───┬───┘└┬┘└──────────────────┬─────────────────────┘ │ │ │ │ │ └ Dritter Bereich: Wieder │ │ von ' umschlossen, der │ │ Backlash verliert │ │ seine Sonderbedeutung. │ │ │ └ Zweiter Bereich: Von " umschlossen, enthält ein │ einzelnes '. │ └ Erster Bereich: Von ' umschlossen.</pre></section> <section> <h3>Input - Output redirection</h3> <pre><code>#!/bin/bash # > : output in Datei. Best. Datei wird überschrieben! ls > file_list # >> : output an Datei anhängen. ls -la >> file_list # < : Kommando Input aus Datei lesen cat < file_list</code></pre> </section> <section> Man kann Kommandos auch verkettten: <table style="font-size: .5em"> <tbody> <tr> <td >Kommando1; Kommando2 </td> <td >fühert die Kommandos hintereinander aus </td> </tr> <tr> <td >Kommando1 && Kommando2 </td> <td >führt Kommando2 aus, wenn kommando1 erfolgreich war </td> </tr> <tr> <td >Kommando1 || Kommando2 </td> <td >führt Kommando2 aus, wenn kommando1 NICHT erfolgreich war </td> </tr> <tr> <td >Kommado & </td> <td >Führt Kommando im Hintergrund aus </td> </tr> <tr> <td >Kommando1 & Kommando2 </td> <td >startet Kommando1 im Hintergrund, Kommando2 im Vordergrund </td> </tr> <tr> <td >( Kommando1; Kommando2 ) </td> <td >Führt die beiden K. nacheinander in einer Subshell aus </td> </tr> </tbody> </table> <pre class="fragment"><code>sudo apt-get update && sudo apt-get upgrade -y tar cf archiv.tar && rm *.doc || echo "fehler"</code></pre> </section> <section> <span style="color: orange">Aufgabe:</span> Erstellt ein script, das den Inhalt eures home in eine Variable <code>liste</code> schreibt, und gebt den Inhalt dieser Variablen in eine Textdatei <code>liste.txt</code> aus.<br /> Hinweis: ls , tee <pre class="fragment"><code>#!/bin/bash OUTFILE="liste.txt" liste=`ls` echo $liste > $OUTFILE #mit tee in einer Zeile liste=`ls | tee $OUTFILE` cat $OUTFILE </code></pre> </section> <section> <h3>exit status</h3> <pre><code>exit 0</code></pre> heisst : alles gut gelaufen <pre><code>exit 1</code></pre> heisst : kommando fehlgeschlagen <p> der exit code in scripten kann beliebig gesetzt werden <p>Standard: exit 0 -> alles fein </section> <section> <h3>Funktionen</h3> <pre><code>#!/bin/bash #Funktions Deklaration: count () { ls | wc -l # ls: Liste aller Objekte im Verzeichnis # wc: Word-Count; mit Attribut -l werden Zeilen gezählt # in Verbindung mit ls werden also die # (nicht versteckten) Objekte gezählt } count # Aufruf der Funktion </code></pre> </section> <section> <pre><code>#!/bin/sh fatal() { msg=$1 echo "Fatal: $msg" exit 1 } [ -d /tmp ] || fatal "Verzeichnis /tmp existiert nicht" [ -w /tmp ] || fatal "Verzeichnis /tmp nicht schreibbar" TMP=/tmp/mydir [ -d $TMP ] || mkdir $TMP # tmp-Verz. erzeugen, wenn noch nicht vorhanden </code></pre> </section> <section> <pre><code>#!/bin/bash count2 () { # überprüfen, ob der erste Parameter ein Verzeichnis ist if [ -d "$1" ]; then ls $1 | wc -l exit 0 # alles OK else echo "Ungültiges Verzeichnis: $1" exit 1 # Fehler fi } count2 "/gibt/es/garnicht" # Aufrufe der Funktion count2 echo "Status: $?" count2 "/etc" echo "Status: $?"</code></pre> </section> <section style="font-size: .8em"> <span style="color: orange">Aufgabe:</span> Erstellt script mit mind einer Funktion das: <ul> <li>ein Backup eines Verzeichnisses erstellt</li> <li>per Parameter den namen des Quell-Verzeichnis erhält</li> <li>per Parameter den Namen der Zieldatei erhält</li> <li>einen nützlichen exit code setzt</li> <li>vielleicht nützliche Meldungen ausgibt</li> </ul> <div><p>Tipps:</p> <ul> <li>Parameter Übergabe: kommando param1 param2</li> <li>Parameter Übernahme: param1 = $1 , param2 = $2 etc</li> </ul> </div> </section> <section style="font-size: .8em"> <pre><code>#!/bin/bash function file_backup { echo 'Backup --- building tar File...' tar -czf $2 $1 || fail "Fehler beim Archiv erstellen" echo '+DONE' exit 0 } fail () { echo "Fehler! + $1" exit 1 } #function call # file_backup 'backup-quelle' 'backup-ziel.tar.gz' file_backup $1 $2 </code></pre> <aside class="notes"> hinzufügen überprüfung ob quelle verz. <br> überprüfung ob ziel existiert <br> überpr. ob ziel angelegt <br> überprüf ob param vorhanden <br> backup verz anlegen </aside> </section> <section> <h4>Script Kontrolle</h4> <ul> <li class="fragment">CTRL + C - Abbbruch</li> <li class="fragment">CTRL + Z - Pausieren</li> <li class="fragment"><code>fg</code> - Fortsetzen</li> <li class="fragment"><code>bg</code> - im Hintergrund Fortsetzen</li> <li class="fragment">CTRL + D - Eingabe beenden</li> </ul> </section> <section> <h4>Job Control</h4> <pre><code> #!/bin/bash find / -name *lib* 2>/dev/null > libs.txt # CTRL - Z jobs fg # CTRL - Z bg </code></pre> <aside class="notes"> cat > test.txt<br> dd<br> ff<br> gg<br> CTRL - D </aside> </section> <section> <pre><code> Integer Berechnungen let "m = 4 * 1024";echo $m let "m += 15" let "m -= 3" let "m /= 5" let "m %= 10" let "m++" let "m--" </code></pre> <pre><code> Floating Point: echo "32.0 + 1.4" | bc echo `expr $m + 18` m=`expr $m + 18` (( m *= 4 )) </code></pre> </section> <section>ternary operator <pre><code>let "k = (m < 9) ? 0 : 1" condition ? value-if-true : value-if-false</code></pre></section> <section data-transition="slide" data-background="#4d7e65" data-background-transition="zoom"> <h3>Maus ist voll 90er !!</h3> <a href="https://github.com/agarrharr/awesome-cli-apps">https://github.com/agarrharr/awesome-cli-apps</a> <a href="https://github.com/alebcay/awesome-shell">https://github.com/alebcay/awesome-shell</a> </section> </div> </div> <script src="lib/js/head.min.js"></script> <script src="js/reveal.js"></script> <script> // More info https://github.com/hakimel/reveal.js#configuration Reveal.initialize({ controls: true, progress: true, history: true, center: true, transition: 'slide', // none/fade/slide/convex/concave/zoom // More info https://github.com/hakimel/reveal.js#dependencies dependencies: [ { src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } }, { src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } }, { src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } }, { src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, { src: 'plugin/search/search.js', async: true }, { src: 'plugin/zoom-js/zoom.js', async: true }, { src: 'plugin/notes/notes.js', async: true } ] }); </script> </body> </html>