Bash

Aus M740 WikiWeb

Wechseln zu: Navigation, Suche

Tips und Tricks mit dem Unix Shell-Programm bash.

Inhaltsverzeichnis

Syntax

Kommandos

command  := [assignment]... [command [parameter]...] [redirections]
pipeline := [time [-p]] [ ! ] command [ | command2 ... ]
list     := pipeline [operator list]
operator := ';' | '&' | '&&' | '||' | LF

Verbund-Kommandos

(list)           : Eine Liste, die in einer Untershell abgearbeitet wird.
{ list; }        : Ein Gruppierung
((expression))   : Arithmetischer Ausdruck
[[ expression ]] : Boolscher Ausdruck, OP = '()', '!', '&&', '||'

Kontrollstrukturen

for name [ in word ] ; do list ; done
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
select name [ in word ] ; do list ; done
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
if list; then list; [ elif list; then list; ] ... [ else list; ] fi
while list; do list; done
until list; do list; done
[ function ] name () { list; }

Ausgabe Umleitung

Dieses kleine Script (/local/bin/testoutput) erzeugt je eine Zeile auf stdout und stderr:

#!/bin/sh
echo "stdout"
echo "stderr" 1>&2

Ausgabeumleitung in Dateien

testoutput >out.txt		# stdout to file
testoutput 2>err.txt		# stderr to file
testoutput >out.txt 2>err.txt	# stdout and stderr to different files
testoutput 1>&2			# stdout to stderr
testoutput 2>&1			# stderr to stdout
testoutput >both.txt 2>&1	# stderr and stdout to file
testoutput &>both.txt		# stderr and stdout to file

Ausgabeumleitung mit Pipes

testoutput 2>&1 | tee dump		# stderr and stdout to pipe
testoutput 2>&1 >out.txt | tee dump	# stdout to file and stderr to pipe
testoutput 2>err.txt | tee dump		# stdout to pipe and stderr to file

Eine Anmerkung zu 2>&1: Es bedeutet nicht: »kopiere stderr nach stdout«, sondern: »Verwende für stderr das aktuelle Ziel von stdout«. Denn wenn nach 2>&1 das Ziel von stdout verändert wird bleibt das Ziel von stderr unverändert. Und deswegen funktoniert auch Beispiel #2.

Web Links

Persönliche Werkzeuge