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

read

Read one line of input (from the keyboard, a file or a pipe) into variables.

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

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

ماذا يفعل

read NAME waits for a line and stores it in NAME. With more names, the words are split: the first word to the first name, the rest to the last one. With no name the line goes to $REPLY. while read -r LINE; do …; done < FILE is the standard way to process a file line by line.

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

read [-ers] [-a ARRAY] [-d DELIM] [-i TEXT] [-n N] [-N N] [-p PROMPT] [-t TIMEOUT] [-u FD] [NAME]...

الخيارات 11

الأكثر استخدامًا

-p PROMPT
Show a prompt first.
-r
Backslashes are normal characters (use it almost always).
-s
Silent: do not show the typed text (passwords).
-t SECONDS
Give up after this time.
-n N
Return after N characters.
-N N
Read exactly N characters.
-a ARRAY
Store the words in an array.
-d DELIM
Read until this character instead of a newline.
-e
Use readline (editing keys) in a terminal.
-i TEXT
Default text for -e.
-u FD
Read from this file descriptor.

أمثلة 10

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Ask a question

    read -p "Your name: " NAME; echo "Hello $NAME"

    Simple input in scripts.

  2. 2Ask for a password

    read -s -p "Password: " PASS; echo

    Nothing is shown while typing.

  3. 3Process a file line by line

    while read -r LINE; do echo "line: $LINE"; done < /etc/hostname

    -r keeps backslashes.

  4. 4Split fields

    echo "anna sales 3000" | { read -r NAME GROUP GID; echo "$NAME is in $GROUP ($GID)"; }

    Words go to the names in order.

  5. 5Split /etc/passwd fields

    while IFS=: read -r USER _ UID _; do [ "$UID" -ge 1000 ] && echo "$USER"; done < /etc/passwd

    IFS=: splits on colons.

  6. 6Yes/no with a timeout

    read -t 10 -n 1 -p "Continue? [y/N] " ANSWER; echo; [ "$ANSWER" = y ] || exit 1

    Defaults to no after 10 seconds.

  7. 7Into an array

    read -r -a PARTS <<< "a b c"; echo "${PARTS[1]}"
    b

    Index starts at 0.

  8. 8Read command output (not a pipe into while)

    while read -r PID CMD; do echo "$PID $CMD"; done < <(ps -eo pid=,comm= | head -3)

    Process substitution keeps variables after the loop.

  9. 9Default value with editing

    read -e -i "/backup" -p "Target folder: " DIR

    The user can edit the default.

  10. 10Pause a script

    read -r -p "Press Enter to continue"

    Waits for Enter.

انتبه

  • Without -r, backslashes in the input disappear.
  • cmd | while read … runs the loop in a subshell: variables set inside are gone afterwards.

رموز الخروج

0 :: A line was read. 1 :: End of file (or timeout > 128 with -t).

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man read