Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

test

Check a condition (file exists, strings equal, number greater …) and return 0 (true) or 1 (false). Also written as [ … ].

Niveau
RHCSA-examen
Groep
Shell en hulp
Root nodig
Nee
Pakket
bash (built-in) / coreutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

test EXPRESSION and [ EXPRESSION ] are the same; the ] is required and needs spaces around it. They are used with if, while, && and ||. Bash also has [[ … ]], which adds pattern matching (== with *), regular expressions (=~) and safer handling of empty variables.

Zo schrijf je het

test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]

Belangrijkste delen

Files

-e FILE
Exists (any type).
-f FILE
Is a regular file.
-d FILE
Is a folder.
-L FILE, -h FILE
Is a symbolic link.
-b FILE, -c FILE
Block / character device.
-p FILE, -S FILE
Named pipe / socket.
-s FILE
Exists and is not empty.
-r FILE, -w FILE, -x FILE
Readable / writable / executable for you.
-u FILE, -g FILE, -k FILE
SUID / SGID / sticky bit set.
-O FILE, -G FILE
Owned by you / by your group.
-N FILE
Changed since last read.
FILE1 -nt FILE2, FILE1 -ot FILE2
Newer / older than.
FILE1 -ef FILE2
Same file (hard link).

Strings

-z STRING
Empty.
-n STRING
Not empty.
S1 = S2, S1 != S2
Equal / not equal.
S1 S2, S1 S2
Sort order (escape in [ ]: \<).
S =~ REGEX
Matches a regular expression (only [[ ]]).

Numbers

N1 -eq N2, -ne
Equal / not equal.
N1 -lt N2, -le
Less / less or equal.
N1 -gt N2, -ge
Greater / greater or equal.

Logic

! EXPR
Not.
EXPR1 -a EXPR2, EXPR1 -o EXPR2
And / or (old; better: [ A ] && [ B ]).
&&, ||
And / or inside [[ ]].
-v NAME
The variable is set (bash).

Voorbeelden 11

$ als gewone gebruiker# als root (of met sudo)

  1. 1Does a file exist?

    [ -f /etc/fstab ] && echo "yes"
    yes

    Spaces inside the brackets are required.

  2. 2In an if

    if [ -d /backup ]; then echo "backup folder ok"; else mkdir /backup; fi

    -d = folder.

  3. 3Compare strings

    [ "$USER" = "root" ] || echo "run as root"

    Quote variables: empty values break [ = root ].

  4. 4Compare numbers

    [ $(df --output=pcent / | tail -1 | tr -dc 0-9) -gt 80 ] && echo "disk almost full"

    -gt for numbers, not >.

  5. 5Check the number of arguments

    set -- a b; [ $# -eq 2 ] && echo "two arguments"

    Common at the start of scripts.

  6. 6Empty variable

    NAME=""; [ -z "$NAME" ] && echo "empty"

    -n for not empty.

  7. 7Pattern match with [[ ]]

    FILE=report.csv; [[ $FILE == *.csv ]] && echo "CSV file"

    Wildcards work only in [[ ]].

  8. 8Regular expression

    IP=192.168.122.10; [[ $IP =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] && echo "looks like an IPv4"

    =~ in [[ ]].

  9. 9Find world-writable scripts (security)

    for f in /usr/local/bin/*; do [ -w "$f" ] && [ "$(stat -c %a "$f" | cut -c3)" -ge 2 ] && echo "$f is world-writable"; done

    Anybody could change these scripts.

  10. 10Is a file newer than another?

    [ /etc/nginx/nginx.conf -nt /var/run/nginx.pid ] && echo "config changed since start"

    Reload needed.

  11. 11Exit status of test

    test 5 -gt 3; echo $?
    0

    0 = true, 1 = false.

Let op

  • [ $VAR = x ] fails when VAR is empty; write [ "$VAR" = x ].
  • > inside [ ] is a redirect, not a compare. Use -gt for numbers.

Exitcodes

0 :: The expression is true. 1 :: The expression is false. 2 :: Error in the expression.