Donnerstag, 24. September 2009
Übungsbeispiel bash - Lösungen
Eine kleine Übung für die Verwendung von
Laden Sie sich die Datei suchedateien.tgz herunter und packen Sie dieses Archiv aus.
Mit welcher Kommandozeile (jeweils eine pro Aufgabe) kann man folgende Informationen ermitteln?
Hinweis: Die Lösungen für die Beispiele 3 und 9 kann man noch verbessern.
Der Trick besteht darin, den "Input File Separator" IFS auf den Doppelpunkt zu setzen und dann immer nur das erste Element auszugeben. Das ganze in einer Schleife.
Dies ist aber sehr fortgeschrittenes
bash
und Linux/Unix-Befehlen und die Verkettung mit Pipes.Laden Sie sich die Datei suchedateien.tgz herunter und packen Sie dieses Archiv aus.
$ tar xzf suchedateien.tgz
$ cd hp
Mit welcher Kommandozeile (jeweils eine pro Aufgabe) kann man folgende Informationen ermitteln?
- Liste von Dateien mit der Endung
txt
$ find . -name '*.txt'
./beisp.txt
./retabTest.txt
./strbsp.txt
./vim.txt
./dateien.txt
./probetest.txt
./camel.pl.txt
./gruppenwechsel/readme.txt
./testsitzplan.txt
./getline_testdaten.txt - Anzahl der Dateien mit der Endung
txt
$ find . -name '*.txt'|wc -l
10 - Liste von Textdateien (das sind nicht nur Dateien mit der Endung
txt
sondern z.B. auch Source-Files)$ find .|xargs file|grep -i text
...viele Dateien...
./zz.c: ISO-8859 C program text
./2-dim-array.html: HTML document text
./stil.c: ASCII C program text
./hauf.c~: ISO-8859 C program text
./kreis.c~: ISO-8859 C program text
./int-div.c~: ASCII C program text./kreis.c.ps: PostScript document text conforming DSC level 3.0
./ziffern.c: ISO-8859 C program text
./dnf_knf.c~: ISO-8859 C program text
./readint.c~: ISO-8859 C program text
./getline_testdaten.txt: ASCII text
./optionen.c: ISO-8859 C program text - Anzahl der Textdateien
$ find .|xargs file|grep -i text|wc -l
250 - Liste von Dateien mit der Endung
c
$ find . -name '*c'
...viele Dateien...
./zz.c
./stil.c
./box.c
./hauf.c
./banner.c
./demostderr.c
./alloc.c
./getopt.c
./floathex.c
./stringfehler.c - Anzahl der Dateien mit der Endung
c
$ find . -name '*c'|wc -l
121 - Liste der Dateien mit der Endung
c
, die nicht den NamenHaberstroh
enthalten$ find . -name '*c'|xargs grep -L -i haberstroh
./floathex.c
./dateien-abc.sxc
./patmatch.c
./ostern.c
./alloc
./einZeichen.c
./artikelc
./wochentag-unformatiert.c
./dnf_knf.c
./hanoi-gr.c - Anzahl der Dateien mit der Endung
c
, die nicht den NamenHaberstroh
enthalten$ find . -name '*c'|xargs grep -L -i haberstroh|wc -l
10 - Liste der Dateien, die keine Textdateien sind
$ find . |xargs file|grep -vi text
...viele Dateien...
.: directory
./dir: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.2.5, not stripped
./upn: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.2.5, not stripped
./wzz: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.2.5, not stripped
./hauf: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.2.5, not stripped
./stdio: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.2.5, not stripped
./strukturbsp: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.2.5, not stripped
./sort4z: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.2.5, not stripped
./komplex: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.2.5, not stripped
./tictactoe-ai-new: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.2.5, not stripped - Anzahl der Dateien, die keine Textdateien sind
$ find . |xargs file|grep -vi text|wc -l
118
Hinweis: Die Lösungen für die Beispiele 3 und 9 kann man noch verbessern.
file
gibt ja die Information in der Form ./komplex: ELF 32...
aus. Wenn wirklich nur die Dateinamen gewünscht sind, könnte man für Beispiel 9 folgenden Befehl angeben:$ find . |xargs file|grep -vi text|while read x;do IFS=":";set $x;echo $1;done...viele Dateien...
.
./dir
./upn
./wzz
./hauf
./stdio
./strukturbsp
./sort4z
./komplex
./tictactoe-ai-new
Der Trick besteht darin, den "Input File Separator" IFS auf den Doppelpunkt zu setzen und dann immer nur das erste Element auszugeben. Das ganze in einer Schleife.
Dies ist aber sehr fortgeschrittenes
bash
-Programmieren und geht weit über das in der Schule benötigte Wissen hinaus.Abonnieren Posts [Atom]
Kommentar veröffentlichen