Wiki source for Bash
=====BASH=====
Weiterführende [[BashLinks Links]]
Seite zum Thema [[LinuxStreamProcessing Streamverarbeitung]]
==a==Standard-IO, Error-IO, File-IO und Pipes==a==
||A||Befehl ausführen und auf Beendigung warten ||
||A & || Befehl im Hintergrund ausführen||
||A ; B ||Befehl A ausführen, dann Befehl B||
||(A; B)||Befehle als Gruppe in einer Shell ausgeführt||
||A ""|"" B || Pipe; Die Ausgabe von A wird an B als Eingabe weitergeleitet||
||A $(B) || Die Ausgabe von B wird zu Befehlsargumenten von A||
||A && B || B wird ausgeführt, wenn A ohne Fehler ausgeführt wurde||
||A ""||"" B|| B wird ausgeführt, wenn bei A ein Fehler aufgetreten ist||
||A > out.txt|| Die Ausgabe (Fehlermeldungen ausgenommen) von A in die Datei out.txt schreiben; Die Datei wird überschrieben||
||A "">>"" out.txt|| Die Ausgabe (Fehlermeldungen ausgenommen) von A an den Inhalt der Datei out.txt anhängen||
||A 2> out.txt|| Die Fehlermeldungen von A an in die Datei out.txt schr iben||
||A < in.txt|| Der Inhalt von in.txt wird als Eingabe an A weitergeleitet||
==a==for-Schleife==a==
Ergebnisse eines Ausdrucks, in dem Fall //ls// in einer Schleife bearbeiten in dem Fall Ausgabe mit //echo//
%%(bash)
for i in * ; do echo "Datei: $i" ; done
%%
Alternative
%%(bash)
for i in `ls` ; do echo "Datei: $i" ; done
%%
Als Skript
%%(bash)
#!/bin/bash
for dot in `ls *.dot`
do
echo $dot
dot -Tpng $dot -o $dot.png
done
%%
Zählen
%%(bash)
for (( i=4; i<20; i++ )); do echo $i ; done
%%
==a==While==a==
%%(bash)
while [ condition ]
do
[Kommandos]
done
%%
Endless loop
%%(bash)
while [ true ]
do
...
done
%%
==a==Variablen==a==
Zahl inkrementieren
%%(bash)
I=1
expr $I + 1
%%
Besser
%%
zahl=1
zahl=$(( $zahl + 1 ))
%%
String zuweisen
%%
set A=Dies ist ein Test
echo %A%
set B=%A%
echo %B%
%%
Pfad hinzufügen
%%
set PATH=%PATH%;C:\rnsget
%%
Set alle Variablen auflisten
%%
set
%%
==a==Vordefinierte Variablen==a==
$0 ist der Skript-Aufruf; z.B. ./scripts/script.sh
$1 ist der erste Übergabeparameter an das Skript
$2 ist der zweite Übergabeparameter
usw.
==a==Tricks==a==
In den Skript-Pfad wechseln
%%(bash)
#Change to script directory
cd `dirname $0`
%%
%%(bash)
#Print working directory full path again
pwd
%%
basename
%%(bash)
# Output working directory name (short)
basename `pwd`
#Strip non-directory suffix from the script name
dirname $0
%%
Merke altes Verzeichnis
%%(bash)
OLDDIR=`pwd` #Save current dir
cd `dirname $0` #Change to script directory or any other directory
# do something else
cd $OLDDIR #Change back to old dir
%%
==a==Variablen-Tests==a==
=a=Zahlenvergleich mit anschließender Programmverzweigung=a=
%%(bash)
VALUE=5
test $VALUE -gt 4 && echo "Größer als 4" || echo "Kleiner oder gleich 4"
%%
=a=Vergleich=a=
%%(bash)
[ "abc" != "xyz" ] ; echo $?
%%
=a=String is not null=a=
%%(bash)
test -n "$DISPLAY" ; echo $?
test -n "$DISPLAY" && echo "OK"
%%
Mit IF
%%(bash)
if [ "$a" -gt "0" ]; then
echo 'true'
else
echo 'false'
fi
%%
=a=Integer comparison=a=
%%(bash)
-eq is equal to
if [ "$a" -eq "$b" ]
-ne is not equal to
if [ "$a" -ne "$b" ]
-gt is greater than
if [ "$a" -gt "$b" ]
-ge is greater than or equal to
if [ "$a" -ge "$b" ]
-lt is less than
if [ "$a" -lt "$b" ]
-le is less than or equal to
if [ "$a" -le "$b" ]
< is less than (within double parentheses)
(("$a" < "$b"))
<= is less than or equal to (within double parentheses)
(("$a" <= "$b"))
> is greater than (within double parentheses)
(("$a" > "$b"))
>= is greater than or equal to (within double parentheses)
(("$a" >= "$b"))
%%
=a=String comparison=a=
%%(bash)
= is equal to
if [ "$a" = "$b" ]
== is equal to
if [ "$a" == "$b" ]
!= is not equal to
if [ "$a" != "$b" ]
This operator uses pattern matching within a [[ ... ]] construct.
< is less than, in ASCII alphabetical order
if [[ "$a" < "$b" ]]
if [ "$a" < "$b" ]
Note that the "<" needs to be escaped within a [ ] construct.
> is greater than, in ASCII alphabetical order
if [[ "$a" > "$b" ]]
if [ "$a" > "$b" ]
Note that the ">" needs to be escaped within a [ ] construct.
See Example 26-11 for an application of this comparison operator.
-z string is "null", that is, has zero length
-n string is not "null".
%%
=a=Compound comparison=a=
%%(bash)
-a logical and
exp1 -a exp2 returns true if both exp1 and exp2 are true.
-o logical or
exp1 -o exp2 returns true if either exp1 or exp2 are true.
%%
=a=Multiple conditions=a=
%%(bash)
Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic
errors in scripts. For example,the &&, ||, <, and > operators work within
a [[ ]] test, despite giving an error within a [ ] construct.
Example:
[[ $umlA -le $umlN && -n $umlN ]]
%%
==a==File tests==a==
%%(bash)
-e file exists
-a file exists
This is identical in effect to -e. It has been "deprecated," and its use is discouraged.
-f file is a regular file (not a directory or device file)
-s file is not zero size
-d file is a directory
-b file is a block device (floppy, cdrom, etc.)
-c file is a character device (keyboard, modem, sound card, etc.)
-p file is a pipe
-h file is a symbolic link
-L file is a symbolic link
-S file is a socket
-t file (descriptor) is associated with a terminal device
This test option may be used to check whether the stdin ([ -t 0 ]) or stdout ([ -t 1 ]) in
a given script is a terminal.
-r file has read permission (for the user running the test)
-w file has write permission (for the user running the test)
-x file has execute permission (for the user running the test)
-g set-group-id (sgid) flag set on file or directory
If a directory has the sgid flag set, then a file created within that directory belongs to the group that
owns the directory, not necessarily to the group of the user who created the file. This may be useful
for a directory shared by a workgroup.
-u set-user-id (suid) flag set on file
-k sticky bit set
-O you are owner of file
-G group-id of file same as yours
-N file modified since it was last read
-nt f1 -nt f2
file f1 is newer than f2
-ot f1 -ot f2
file f1 is older than f2
-ef f1 -ef f2
files f1 and f2 are hard links to the same file
! "not" -- reverses the sense of the tests above (returns true if condition absent).
%%
==a==Parallel processing in BASH==a==
http://stackoverflow.com/questions/13296863/difference-between-wait-and-sleep
%%(bash)
sleep 10 &
wait
%%
==a==BASH error handling==a==
Exit status
%%$?%%
%%(bash)
#!/bin/bash
echo "Test with unknown command should fail."
foofoo 2> /dev/null
exitcode=$?
echo $exitcode
if [ $exitcode -ne 0 ]; then
echo "Error"
else
echo "OK"
fi
echo " "
echo "Test with successfull executed command should return 0."
ls > /dev/null
exitcode=$?
echo $exitcode
if [ $exitcode -ne 0 ]; then
echo "Error"
else
echo "OK"
fi
%%
Result
%%
Test with unknown command should fail.
127
Error
Test with successfull executed command should return 0.
0
OK
%%
==a==Create enumerated files and directories==a==
%%(bash)
mkdir dir{0..9}
%%
dir0
dir1
dir2
...
dir9
==a==Console colors==a==
[[PythonBashConsoleColor Bash color output in Python]]
----
Siehe auch {{backlinks}}
Weiterführende [[BashLinks Links]]
Seite zum Thema [[LinuxStreamProcessing Streamverarbeitung]]
==a==Standard-IO, Error-IO, File-IO und Pipes==a==
||A||Befehl ausführen und auf Beendigung warten ||
||A & || Befehl im Hintergrund ausführen||
||A ; B ||Befehl A ausführen, dann Befehl B||
||(A; B)||Befehle als Gruppe in einer Shell ausgeführt||
||A ""|"" B || Pipe; Die Ausgabe von A wird an B als Eingabe weitergeleitet||
||A $(B) || Die Ausgabe von B wird zu Befehlsargumenten von A||
||A && B || B wird ausgeführt, wenn A ohne Fehler ausgeführt wurde||
||A ""||"" B|| B wird ausgeführt, wenn bei A ein Fehler aufgetreten ist||
||A > out.txt|| Die Ausgabe (Fehlermeldungen ausgenommen) von A in die Datei out.txt schreiben; Die Datei wird überschrieben||
||A "">>"" out.txt|| Die Ausgabe (Fehlermeldungen ausgenommen) von A an den Inhalt der Datei out.txt anhängen||
||A 2> out.txt|| Die Fehlermeldungen von A an in die Datei out.txt schr iben||
||A < in.txt|| Der Inhalt von in.txt wird als Eingabe an A weitergeleitet||
==a==for-Schleife==a==
Ergebnisse eines Ausdrucks, in dem Fall //ls// in einer Schleife bearbeiten in dem Fall Ausgabe mit //echo//
%%(bash)
for i in * ; do echo "Datei: $i" ; done
%%
Alternative
%%(bash)
for i in `ls` ; do echo "Datei: $i" ; done
%%
Als Skript
%%(bash)
#!/bin/bash
for dot in `ls *.dot`
do
echo $dot
dot -Tpng $dot -o $dot.png
done
%%
Zählen
%%(bash)
for (( i=4; i<20; i++ )); do echo $i ; done
%%
==a==While==a==
%%(bash)
while [ condition ]
do
[Kommandos]
done
%%
Endless loop
%%(bash)
while [ true ]
do
...
done
%%
==a==Variablen==a==
Zahl inkrementieren
%%(bash)
I=1
expr $I + 1
%%
Besser
%%
zahl=1
zahl=$(( $zahl + 1 ))
%%
String zuweisen
%%
set A=Dies ist ein Test
echo %A%
set B=%A%
echo %B%
%%
Pfad hinzufügen
%%
set PATH=%PATH%;C:\rnsget
%%
Set alle Variablen auflisten
%%
set
%%
==a==Vordefinierte Variablen==a==
$0 ist der Skript-Aufruf; z.B. ./scripts/script.sh
$1 ist der erste Übergabeparameter an das Skript
$2 ist der zweite Übergabeparameter
usw.
==a==Tricks==a==
In den Skript-Pfad wechseln
%%(bash)
#Change to script directory
cd `dirname $0`
%%
%%(bash)
#Print working directory full path again
pwd
%%
basename
%%(bash)
# Output working directory name (short)
basename `pwd`
#Strip non-directory suffix from the script name
dirname $0
%%
Merke altes Verzeichnis
%%(bash)
OLDDIR=`pwd` #Save current dir
cd `dirname $0` #Change to script directory or any other directory
# do something else
cd $OLDDIR #Change back to old dir
%%
==a==Variablen-Tests==a==
=a=Zahlenvergleich mit anschließender Programmverzweigung=a=
%%(bash)
VALUE=5
test $VALUE -gt 4 && echo "Größer als 4" || echo "Kleiner oder gleich 4"
%%
=a=Vergleich=a=
%%(bash)
[ "abc" != "xyz" ] ; echo $?
%%
=a=String is not null=a=
%%(bash)
test -n "$DISPLAY" ; echo $?
test -n "$DISPLAY" && echo "OK"
%%
Mit IF
%%(bash)
if [ "$a" -gt "0" ]; then
echo 'true'
else
echo 'false'
fi
%%
=a=Integer comparison=a=
%%(bash)
-eq is equal to
if [ "$a" -eq "$b" ]
-ne is not equal to
if [ "$a" -ne "$b" ]
-gt is greater than
if [ "$a" -gt "$b" ]
-ge is greater than or equal to
if [ "$a" -ge "$b" ]
-lt is less than
if [ "$a" -lt "$b" ]
-le is less than or equal to
if [ "$a" -le "$b" ]
< is less than (within double parentheses)
(("$a" < "$b"))
<= is less than or equal to (within double parentheses)
(("$a" <= "$b"))
> is greater than (within double parentheses)
(("$a" > "$b"))
>= is greater than or equal to (within double parentheses)
(("$a" >= "$b"))
%%
=a=String comparison=a=
%%(bash)
= is equal to
if [ "$a" = "$b" ]
== is equal to
if [ "$a" == "$b" ]
!= is not equal to
if [ "$a" != "$b" ]
This operator uses pattern matching within a [[ ... ]] construct.
< is less than, in ASCII alphabetical order
if [[ "$a" < "$b" ]]
if [ "$a" < "$b" ]
Note that the "<" needs to be escaped within a [ ] construct.
> is greater than, in ASCII alphabetical order
if [[ "$a" > "$b" ]]
if [ "$a" > "$b" ]
Note that the ">" needs to be escaped within a [ ] construct.
See Example 26-11 for an application of this comparison operator.
-z string is "null", that is, has zero length
-n string is not "null".
%%
=a=Compound comparison=a=
%%(bash)
-a logical and
exp1 -a exp2 returns true if both exp1 and exp2 are true.
-o logical or
exp1 -o exp2 returns true if either exp1 or exp2 are true.
%%
=a=Multiple conditions=a=
%%(bash)
Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic
errors in scripts. For example,the &&, ||, <, and > operators work within
a [[ ]] test, despite giving an error within a [ ] construct.
Example:
[[ $umlA -le $umlN && -n $umlN ]]
%%
==a==File tests==a==
%%(bash)
-e file exists
-a file exists
This is identical in effect to -e. It has been "deprecated," and its use is discouraged.
-f file is a regular file (not a directory or device file)
-s file is not zero size
-d file is a directory
-b file is a block device (floppy, cdrom, etc.)
-c file is a character device (keyboard, modem, sound card, etc.)
-p file is a pipe
-h file is a symbolic link
-L file is a symbolic link
-S file is a socket
-t file (descriptor) is associated with a terminal device
This test option may be used to check whether the stdin ([ -t 0 ]) or stdout ([ -t 1 ]) in
a given script is a terminal.
-r file has read permission (for the user running the test)
-w file has write permission (for the user running the test)
-x file has execute permission (for the user running the test)
-g set-group-id (sgid) flag set on file or directory
If a directory has the sgid flag set, then a file created within that directory belongs to the group that
owns the directory, not necessarily to the group of the user who created the file. This may be useful
for a directory shared by a workgroup.
-u set-user-id (suid) flag set on file
-k sticky bit set
-O you are owner of file
-G group-id of file same as yours
-N file modified since it was last read
-nt f1 -nt f2
file f1 is newer than f2
-ot f1 -ot f2
file f1 is older than f2
-ef f1 -ef f2
files f1 and f2 are hard links to the same file
! "not" -- reverses the sense of the tests above (returns true if condition absent).
%%
==a==Parallel processing in BASH==a==
http://stackoverflow.com/questions/13296863/difference-between-wait-and-sleep
%%(bash)
sleep 10 &
wait
%%
==a==BASH error handling==a==
Exit status
%%$?%%
%%(bash)
#!/bin/bash
echo "Test with unknown command should fail."
foofoo 2> /dev/null
exitcode=$?
echo $exitcode
if [ $exitcode -ne 0 ]; then
echo "Error"
else
echo "OK"
fi
echo " "
echo "Test with successfull executed command should return 0."
ls > /dev/null
exitcode=$?
echo $exitcode
if [ $exitcode -ne 0 ]; then
echo "Error"
else
echo "OK"
fi
%%
Result
%%
Test with unknown command should fail.
127
Error
Test with successfull executed command should return 0.
0
OK
%%
==a==Create enumerated files and directories==a==
%%(bash)
mkdir dir{0..9}
%%
dir0
dir1
dir2
...
dir9
==a==Console colors==a==
[[PythonBashConsoleColor Bash color output in Python]]
----
Siehe auch {{backlinks}}