103.4 Ströme, Pipes und Umleitungen verwenden

Aus TUXSPACE Wiki
Version vom 12. März 2016, 01:53 Uhr von Hwe (Diskussion | Beiträge)

(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

Objectives

Weight: 4

Description: Candidates should be able to redirect streams and connect them in order to efficiently process textual data. Tasks include redirecting standard input, standard output and standard error, piping the output of one command to the input of another command, using the output of one command as arguments to another command and sending output to both stdout and a file..

Key Knowledge Areas

  • Redirecting standard input, standard output and standard error.
  • Pipe the output of one command to the input of another command.
  • Use the output of one command as arguments to another command.
  • Send output to both stdout and a file.

Terms and Utilities

Umleitungen (Redirects)

Redirects (Umleitungen) werden verwendet um Datenströme in eine Datei umzuleiten oder aus einer Datei heraus umzulenken.

Bezeichnung der Kanäle:

  • 0 (stdin) Standardeingabekanal
  • 1 (stdout) Standardausgabekanal
  • 2 (stderr) Standardfehlerkanal

Allgemeine Syntax:

  • Syntax um den Standardausgabekanal umzulenken
    • Kommando > Zieldatei (überschreibend)
    • Kommando 1> Zieldatei (überschreibend)
    • Kommando >> Zieldatei (anhängend)
    • Kommando 1>> Zieldatei (anhängend)
  • Syntax um den Standardfehlerkanal umzulenken
    • Kommando 2> Zieldatei (überschreibend)
    • Kommando 2>> Zieldatei (anhängend)
  • Syntax um beide Ausgabekanäle umzulenken
    • Kommando > Zieldatei 2>&1 (gemeinsame Zieldatei)
    • Kommando 1> Zieldatei-A 2> Zieldatei-B (getrennte Zieldateien)
    • Syntax um die Standardeingabekanal umzulenken

Pipes

Im Gegensatz zu Redirects lenken Pipes Datenströme nicht in Dateien um oder aus Dateien heraus. Sie nutzen die Ausgabe (STDOUT) eines Programms direkt als Eingabe (STDIN) für ein anderes Programm.

Listing: Redirects

       überschreiben
       foo@dionysos:~$ echo "1 debian" > bsp.dat
       anhängen
       foo@dionysos:~$ echo "2 ubuntu" >> bsp.dat
       anhängen
       foo@dionysos:~$ echo "3 centos" >> bsp.dat

Listing: schreiben auf stdout mit cat

       foo@dionysos:~$ cat bsp.dat
       1 debian
       2 ubuntu
       3 centos

Listing: Pipes

       ersetze Leerzeichen durch das Tabulatorzeichen
       foo@dionysos:~$ cat bsp.dat | tr " " "\t"
       1	debian
       2	ubuntu
       3	centos

tee

Das Kommando tee liest von der STDIN und schreibt seine Ausgabe nach STDOUT und in eine Datei.

Beispiel

Das Verzeichnis ~/lpi enthält drei Dateien

foo@dionysos:~/lpi$ ls
bsp-103.2.dat  bsp-103.3.dat  bsp-103.4.dat

Das Ergebnis des Kommandos ls -l wird sowohl nach STDOUT als auch in die Datei result.dat geschrieben.

foo@dionysos:~/lpi$ ls -l | tee result.dat
insgesamt 4
-rw-r--r-- 1 foo foo 0 Mär 8 06:37 bsp-103.2.dat
-rw-r--r-- 1 foo foo 0 Mär 8 06:37 bsp-103.3.dat
-rw-r--r-- 1 foo foo 27 Mär 7 05:44 bsp-103.4.dat

Nun befinden sich vier Dateien im Verzeichnis ~/lpi.

foo@dionysos:~/lpi$ ls
bsp-103.2.dat  bsp-103.3.dat  bsp-103.4.dat  result.dat

Ausgabe von result.dat mit cat.

foo@dionysos:~/lpi$ cat result.dat 
insgesamt 4
-rw-r--r-- 1 foo foo  0 Mär  8 06:37 bsp-103.2.dat
-rw-r--r-- 1 foo foo  0 Mär  8 06:37 bsp-103.3.dat
-rw-r--r-- 1 foo foo 27 Mär  7 05:44 bsp-103.4.dat

Das Kommando: xargs

HIER WEITER Allgemeine Syntax: tee [OPTION]... [DATEI]... Option short -a -i Option long - -append - -ignore-interrupts Description Anhängen der Ausgabe an eine bereits bestehende Datei tee wird immun gegen Unterbrechungssignale Listing 3: tee hwe@prometheus: sandbox$ ls -l | tee result.txt insgesamt 48 drwxr-xr-x 2 hwe hwe 4096 Feb 24 02:01 bin drwxr-xr-x 2 hwe hwe 4096 Nov 26 01:09 fotos ... hwe@prometheus: sandbox$ ls bin lpi-103.2 lpi-103.4 lpi-103.8 music regex fotos lpi-103.3 lpi-103.7 lpi-104.4 news result.txt 3 vgl.?, tmp