103.4 Ströme, Pipes und Umleitungen verwenden
Inhaltsverzeichnis
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
- tee
- xargs
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