BASH


Weiterführende BashLinks Links
Seite zum Thema LinuxStreamProcessing Streamverarbeitung


1. Standard-IO, Error-IO, File-IO und Pipes


ABefehl ausführen und auf Beendigung warten
A & Befehl im Hintergrund ausführen
A ; B Befehl A ausführen, dann Befehl 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




2. for-Schleife


Ergebnisse eines Ausdrucks, in dem Fall ls in einer Schleife bearbeiten in dem Fall Ausgabe mit echo
for i in * ; do echo "Datei: $i" ; done


Alternative
for i in `ls` ; do echo "Datei: $i" ; done


Als Skript
#!/bin/bash
for dot in `ls *.dot`
do
  echo $dot
  dot -Tpng $dot -o $dot.png
done


Zählen
for (( i=4; i<20; i++ )); do echo $i ; done



3. While


 while [ condition ]
           do
           [Kommandos]
           done


Endless loop
while [ true ]
do
    ...
done


4. Variablen


Zahl inkrementieren
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 


5. Vordefinierte Variablen


$0 ist der Skript-Aufruf; z.B. ./scripts/script.sh
$1 ist der erste Übergabeparameter an das Skript
$2 ist der zweite Übergabeparameter
usw.


6. Tricks


In den Skript-Pfad wechseln
#Change to script directory
cd `dirname $0`


#Print working directory full path again
pwd


basename
# Output working directory name (short)
basename `pwd`
#Strip non-directory suffix from the script name
dirname $0


Merke altes Verzeichnis
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



7. Variablen-Tests


7.1Zahlenvergleich mit anschließender Programmverzweigung

VALUE=5
test $VALUE -gt 4 && echo "Größer als 4" || echo "Kleiner oder gleich 4"


7.2Vergleich

[ "abc" != "xyz" ] ; echo $?


7.3String is not null

test -n "$DISPLAY" ; echo $?
test -n "$DISPLAY" && echo "OK"


Mit IF
if [ "$a" -gt "0" ]; then
               echo 'true'
else
               echo 'false'
fi


7.4Integer comparison

    -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"))


7.5String comparison

    =   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".


7.6Compound comparison

    -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.


7.7Multiple conditions

    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 ]]





8. File tests

    -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).


9. Parallel processing in BASH

10. BASH error handling


Exit status
$?


#!/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


11. Create enumerated files and directories


mkdir dir{0..9}


dir0
dir1
dir2
...
dir9

12. Console colors

Valid XHTML :: Valid CSS: :: Powered by WikkaWiki