Das Programm „test“

Das test-Kommando wird überall dort eingebaut, wo ein Vergleich von Zeichenketten oder Zahlenwerten und eine Überprüfung von Zuständen einer Datei erforderlich sind. So viele Möglichkeiten wie es zum Testen gibt, gibt es dazugehörige Operatoren für „test“.

„test“ heisst auch “ [ „

Das Programm „test“ ist in einer Hinsicht speziell. Das selbe Programm heisst nämlich auch “ [ „. Dies ermöglicht es, dass damit in einem Script einfacher gearbeitet werden kann.  Zu Beginn gleich ein kurzes Beispiel, dass dies verdeutlichen soll:

if test -e /etc/passwd; then
...

Bewirkt das selbe wie:

if [ -e /etc/passwd ]; then
...

Da es sich bei „[“ also um ein Programm handelt, muss davor und danach jeweils mindestens ein Leerschlag vorhanden sein.

Operatoren

Für die LPIC-Prüfung sollten die Operatoren bekannt sein.

Operator     syntax    Description    
-a <FILE>    True if <FILE> exists. (not recommended, may collide with -a for AND, see below)    
-e <FILE>    True if <FILE> exists.    
-f <FILE>    True, if <FILE> exists and is a regular file.    
-d <FILE>    True, if <FILE> exists and is a directory.
-c <FILE>    True, if <FILE> exists and is a character special file.    
-b <FILE>    True, if <FILE> exists and is a block special file.    
-p <FILE>    True, if <FILE> exists and is a named pipe (FIFO).    
-S <FILE>    True, if <FILE> exists and is a socket file.    
-L <FILE>    True, if <FILE> exists and is a symbolic link.    
-h <FILE>    True, if <FILE> exists and is a symbolic link.    
-g <FILE>    True, if <FILE> exists and has sgid bit set.    
-u <FILE>    True, if <FILE> exists and has suid bit set.    
-r <FILE>    True, if <FILE> exists and is readable.    
-w <FILE>    True, if <FILE> exists and is writable.    
-x <FILE>    True, if <FILE> exists and is executable.    
-s <FILE>    True, if <FILE> exists and has size bigger than 0 (not empty).    
-t <fd>      True, if file descriptor <fd> is open and refers to a terminal.    
<FILE1> -nt <FILE2>    True, if <FILE1> is newer than <FILE2> (mtime).
<FILE1> -ot <FILE2>    True, if <FILE1> is older than <FILE2> (mtime).
<FILE1> -ef <FILE2>    True, if <FILE1> is a hardlink to <FILE2>.

Ganze Zahlen vergleichen

Ausdruck              Bedeutung             Liefert wahr (0) zurück, wenn ...
[ var1 –eq var2 ]     (eq = equal)          var1 gleich var2 ist
[ var1 –ne var2 ]     (ne = not equal)      var1 ungleich var2 ist
[ var1 –lt var2 ]     (lt = less than)      var1 kleiner als var2 ist
[ var1 –gt var2 ]     (gt = greater than)   var1 grösser als var2 ist
[ var1 –le var2 ]     (le = less equal)     var1 kleiner oder gleich var2 ist
[ var1 –ge var2 ]     (ge = greater equal)  var1 grösser oder gleich var2 ist

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

*

Diese Seite verwendet Akismet, um Spam zu reduzieren. Erfahre, wie deine Kommentardaten verarbeitet werden..