===== Kleine Beispiele ===== ==== Hello World ==== #!/bin/bash echo hello world! (( Was passiert wohl, wenn man folgendes Skript aufruft: #!/bin/rm echo hello world! )) ==== Beispiel mit if/else ==== #!/bin/bash if test -n "$1" then echo hello $1 else echo hello world! fi ==== Source ==== #!/bin/sh foo=bar cd /tmp echo "foo=$foo" pwd ps f pstree -spu bash ./srctest chmod +x ./srctest ./srctest source ./srctest . ./srctest ==== Rückgabewerte ==== #!/bin/bash ping -c 1 -w 1 "$1" result=$? if test "$result" -eq 0 then echo $1 antwortet elif test "$result" -eq 1 then echo $1 antwortet nicht elif test "$result" -eq 2 then echo $1 nicht gefunden else echo weiss auch nicht fi ==== Beispiel mit && und || ==== #!/bin/bash test -r test.conf && . test.conf test -z "$name" || hello $name ==== Beispiel mit case ==== #!/bin/bash case "$1" in start) echo $0 wird gestartet ;; stop) echo $0 wird gestopt ;; restart) $0 stop $0 start ;; *) echo was soll ich tun exit 1 esac ==== Beispiel mit for ==== #!/bin/bash for ip in 10 11 12 13 14 do ping -q -c 1 -W 1 192.168.1.$ip >/dev/null && echo 192.168.1.$ip done ==== Beispiel mit for, seq und Kommandosubstitution ==== ( und einer kleinen Subshell ) #!/bin/bash for ip in $(seq 1 254) do ( ping -q -c 1 -W 1 192.168.1.$ip >/dev/null && echo 192.168.1.$ip ) & done ==== Beispiel mit while ==== #!/bin/bash arp -n -a | while read name ip auf mac rest do echo $ip -\> $mac done ==== Beispiel mit while, case und getopt ==== #!/bin/bash von=1 bis=10 subnetz= while getopts 'v:b:s:' opt do case "$opt" in v) von=$OPTARG ;; b) bis=$OPTARG ;; s) subnetz=$OPTARG ;; esac done echo -- -v: $von, -b: $bis, -s: $subnetz ==== Beispiel mit grep, regulärem Ausdruck, id und xargs ==== (Alle Benutzer und Gruppenzugehörigkeit herausfiltern) #!/bin/bash grep -o '^[^:]*' /etc/passwd | xargs -L1 id ===== Etwas größeres Beispiel ===== ==== Suche Mac-Addressen mit mehreren IP-Adressen im Subnetz ==== #!/bin/bash # Default Werte von_ip=1 bis_ip=254 # Subnetz, von ip und bis ip aus Konfigdatei lesen test -r mac_search.conf && . mac_search.conf # Kommandozeilenargumente auswerten while getopts 'v:b:s:' opt do case "$opt" in v) von_ip=$OPTARG ;; b) bis_ip=$OPTARG ;; s) subnetz=$OPTARG ;; esac done # Mac-Adressen für alle aktiven IP-Adressen von $1 bis $2 ausgeben function get_mac { for ip in $(seq $1 $2) do # mac-Adresse von erfolgreich gepingten IP-Adressen ausgeben ( ping -q -c 1 -W 1 $3.$ip >/dev/null && arp -a $3.$ip ) & done while test "$(jobs -r|wc -l)" -ne 0 do sleep 1 done } # Doppelte Mac-Adressen ausfiltern function print_double { while read name ip auf mac rest do echo $mac done | sort | uniq -d } get_mac $von_ip $bis_ip $subnetz | print_double ====== Links zum Shell-Programmieren ====== * Grundlagen: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html * Advanced Bash Scripting: http://tldp.org/LDP/abs/html/ * Nochmal Grundlagen - anders erklärt: http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html * AWK: http://www.gnu.org/manual/gawk/html_node/Getting-Started.html (muss man nicht haben, meistens reicht es, print zu kennen!)