103.4 Ströme, Pipes und Umleitungen verwenden: Unterschied zwischen den Versionen

Aus TUXSPACE Wiki
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „Category:LPIC-1Category:ToDo == Objectives == '''Weight''': 4 '''Description''': Candidates should be able to redirect streams and connect them in ord…“)
 
 
(2 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt)
Zeile 70: Zeile 70:
  
 
== [[tee]] ==
 
== [[tee]] ==
Das Kommando tee liest von der STDIN und schreibt seine Ausgabe nach STDOUT und in eine Datei.
+
== [[xargs]] ==
 
+
'''Beispiel'''
+
 
+
Das Verzeichnis ~/lpi enthält drei Dateien
+
<pre>
+
foo@dionysos:~/lpi$ ls
+
bsp-103.2.dat  bsp-103.3.dat  bsp-103.4.dat
+
</pre>
+
 
+
Das Ergebnis des Kommandos '''ls -l''' wird sowohl nach STDOUT als auch in die Datei ''result.dat'' geschrieben.
+
 
+
<code><span style="color:#ff0000;">foo@dionysos:~/lpi$ ls -l | tee result.dat </span><br/>
+
insgesamt 4<br/>
+
-rw-r--r-- 1 foo foo  0 Mär  8 06:37 bsp-103.2.dat<br/>
+
-rw-r--r-- 1 foo foo  0 Mär  8 06:37 bsp-103.3.dat<br/>
+
-rw-r--r-- 1 foo foo 27 Mär  7 05:44 bsp-103.4.dat<br/>
+
</code>
+
 
+
Nun befinden sich vier Dateien im Verzeichnis ~/lpi.
+
<pre>
+
foo@dionysos:~/lpi$ ls
+
bsp-103.2.dat  bsp-103.3.dat  bsp-103.4.dat  result.dat
+
</pre>
+
 
+
Ausgabe von ''result.dat'' mit '''cat'''.
+
<pre>
+
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
+
</pre>
+
 
+
== 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
+

Aktuelle Version vom 12. März 2016, 10:46 Uhr

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

xargs