الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

test

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

المستوى
امتحان RHCSA
المجموعة
الطرفية والمساعدة
يحتاج root
لا
الحزمة
bash (built-in) / coreutils

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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.

طريقة الكتابة

test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]

الأجزاء الرئيسية

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

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع 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.

انتبه

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

رموز الخروج

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