-_-_ Seite 1 Shell-Scripting Raphael Becker ____ _ _ _ ____ _ _ _ / ___|| |__ ___| | | / ___| ___ _ __(_)_ __ | |_(_)_ __ __ _ \___ \| '_ \ / _ \ | |____\___ \ / __| '__| | '_ \| __| | '_ \ / _` | ___) | | | | __/ | |_____|__) | (__| | | | |_) | |_| | | | | (_| | |____/|_| |_|\___|_|_| |____/ \___|_| |_| .__/ \__|_|_| |_|\__, | |_| |___/ ... erklaert an vielen Beispielen. by Raphael Becker -_-_ Seite 2 Shell-Scripting Raphael Becker Uebersicht ========== * Was sind ueberhaupt Shellscripte? (Kommandos ...) * Fuer was sind sie nuetzlich (zB komplexe Aufgaben ...) * Vorteile (sehr flexibel, verbreitet ...) * Nachteile (zB langsam, ...) * Sind gut zu verwenden bei ... (zB wiederkehrende Aufgaben) * eher nicht gut zu verwenden bei... (zB FIXME) -_-_ Seite 3 Shell-Scripting Raphael Becker Grundlagen ========== * Was benoetige ich (Editor, ...) * hash bang slash bin slash bash oder #!/bin/bash * Variablen * Funktionen -_-_ Seite 4 Shell-Scripting Raphael Becker einfache Beispiele ================== * hello world: code: #!/bin/bash echo hello, world i/o: $ helloworld hello, world * Zaehlen bis 10: code: #!/bin/bash for X in 1 2 3 4 5 6 7 8 9 10; do echo $X done i/o: $ zaehlenbis10 1 2 ... 9 10 -_-_ Seite 5 Shell-Scripting Raphael Becker einfache Beispiele ================== * Zaehlen bis 424242 code: #!/bin/bash for X in $(seq 1 424242); do echo $X done i/o: $ zaehlenbis424242 1 2 ... 424240 424241 424242 * Ausgabe umdrehen code: #!/bin/bash tac | rev i/o: $ reverse hello, world! kleiner Testtext ^D txettseT renielk !dlrow ,olleh -_-_ Seite 6 Shell-Scripting Raphael Becker komplexere Beispiele ==================== code: #/bin/bash # Uhrzeit, bei der geweckt werden soll # Format: "HH:MM", einstellige Stunden mit führender 0!! ALARM="06:34" while true; do clear if [ `date +%H:%M` == $ALARM ]; then echo -e "\a" else echo -e "Alarm um:\t$ALARM" fi echo -e "Aktuelle Zeit:\t`date +%H:%M:%S`" sleep 1 done i/o: $ wecker Alarm um: 06:34 Aktuelle Zeit: 06:50:04 ^C code: http://rabe.uugrn.org/CCC/contest/hanoi2 Dieses Script loest das Problem "Tuerme von Hanoi", rekursive Funktionen! code: http://rabe.uugrn.org/CCC/contest/hanoi2_min_sig Das gleiche Script, allerdings 4x72-crunch. -_-_ Seite 7 Shell-Scripting Raphael Becker komplexere Beispiele mit Webzugriff =================================== code: http://rabe.uugrn.org/scripts/mac #!/bin/sh # by Raphael H. Becker # # This little script reads a MAC-Adress from stdin and # searches for the manufactor of the NIC. This script uses # http://standards.ieee.org/regauth/oui/oui.txt for it. # This Textfile ist about some hunded of kB so this file # will be cached on the first run. FILE=~/.mac.dat URL=http://standards.ieee.org/regauth/oui/oui.txt if [ $1 ] then if [ ! -f $FILE ] then # File not found, download it (>600kB!): lynx -dump $URL >$FILE fi grep -i $(echo $1|sed -e 's/:/-/g'|cut -f 1-3 -d "-") $FILE|cut -c 33- else echo "Usage: $0 " fi -_-_ Seite 8 Shell-Scripting Raphael Becker komplexere Beispiele mit Webzugriff =================================== code: http://rabe.uugrn.org/scripts/dict2 #!/bin/sh # comments removed, look at the URL above for fully commented script t(){ while [ -n "$1" ]; do T=/tmp/$$.html lynx -source "http://dict.leo.org/?search=$1"|\ grep results >$T w3m -dump $T rm $T shift done } [ -n "$1" ]&&\ t "$@"||\ while read -ep dict2\> W; do t $W|more done -_-_ Seite 9 Shell-Scripting Raphael Becker komplexere Beispiele mit Web-Zugriff ==================================== Das selbe script kann man auch gut 4x72 crunchen: code: http://rabe.uugrn.org/scripts/dict2_signature t(){ while [ -n "$1" ];do T=/tmp/$$.html;lynx -source "http://dict.leo.\ org/?search=$1"|grep results >$T&&w3m -dump $T;rm $T;shift;done;} [ -n "$1" ]&&t "$@"||while read -ep dict2\> W;do t $W|more; done #by rhb code: http://rabe.uugrn.org/scripts/heisenews Script, welches per cron aufgerufen die aktuelle News-Uebersicht von heise.de fetcht, dabei nur den relevanten Teil aus dem html-Code ausschneidet, mit lynx parst und per mail verschickt. (Pipes, { ... }-Gruppen, ...) code: http://rabe.uugrn.org/scripts/heisefeed Script, was per cron aufgerufen neue News bei heise.de abruft, sich dabei merkt welche bereits abgerufen und verschickt wurden und Mehrfachzugriffe aus einem Cache bedient. Auf der Uebersichtsseite werden die Links zu den Artikeln rausgesucht und werden als "Druckansicht" geladen und durch lynx gedumpt auf plain/text mit 72 Spalten und per mail verschickt. (Funktionen, Web-Zugriff -_-_ Seite 10 Shell-Scripting Raphael Becker komplexere Beispiele mit Netzzugriff ==================================== code: http://rabe.uugrn.org/scripts/tvhpt Script, welches mittels netcat http mit einem Proxy-Server spricht, dabei interessante features zweckentfremdet. -_-_ Seite 11 Shell-Scripting Raphael Becker * Fragen? -_-_ Seite 12 Shell-Scripting Raphael Becker _ _ __| | __ _ ___ __ ____ _ _ __( )___ / _` |/ _` / __| \ \ /\ / / _` | '__|// __| _ _ _ | (_| | (_| \__ \ \ V V / (_| | | \__ \ (_|_|_) \__,_|\__,_|___/ \_/\_/ \__,_|_| |___/ _____ _ _ ____ _____ | ____| \ | | _ \| ____| | _| | \| | | | | _| | |___| |\ | |_| | |___ |_____|_| \_|____/|_____|