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

معمل 19.1RHCSA 10, الفصل 19

Write admin scripts

المستوى
متوسط
40 دقيقة
الأجهزة
server1
الحالة
لم يبدأ

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

الهدف

Write three small scripts that use arguments, tests, loops and case — the kind of task the exam and daily work ask for.

قبل أن تبدأ

  • Work on server1 as root. Put the scripts in /usr/local/bin/.

المهام

  1. Script mkusers.sh: reads a file given as the first argument, with one user name per line, and creates each user if it does not exist yet. Without an argument it prints a usage line and exits with code 1.
  2. Script diskcheck.sh: prints "WARNING: / is NN% full" when the root file system is more than 80 % full, else "OK". It takes the limit as an optional argument (default 80).
  3. Script web.sh: with the argument start, stop or status it does that with httpd; with anything else it prints a usage line and exits with 1.
  4. Check the syntax of all three scripts and run one of them with tracing.

تحقّق من عملك

printf 'tom\njerry\ntom\n' > /tmp/names.txt
mkusers.sh /tmp/names.txt; id tom; id jerry
mkusers.sh; echo $?
diskcheck.sh; diskcheck.sh 1
web.sh status; web.sh foo; echo $?
  • tom and jerry exist; the second "tom" gives no error.
  • Without arguments the scripts print a usage line and $? is 1.
  • diskcheck.sh 1 prints a WARNING.

تلميحات

اعرض التلميحات

الحل

اعرض الحل
  1. mkusers.sh
    /usr/local/bin/mkusers.sh
    #!/bin/bash
    if [ $# -lt 1 ] || [ ! -f "$1" ]; then
        echo "usage: $0 FILE" >&2
        exit 1
    fi
    while read -r NAME; do
        [ -z "$NAME" ] && continue
        if id "$NAME" &>/dev/null; then
            echo "$NAME exists"
        else
            useradd "$NAME" && echo "$NAME created"
        fi
    done < "$1"
  2. diskcheck.sh
    /usr/local/bin/diskcheck.sh
    #!/bin/bash
    LIMIT=${1:-80}
    USED=$(df --output=pcent / | tail -1 | tr -dc '0-9')
    if [ "$USED" -gt "$LIMIT" ]; then
        echo "WARNING: / is ${USED}% full"
    else
        echo "OK"
    fi
  3. web.sh
    /usr/local/bin/web.sh
    #!/bin/bash
    case "$1" in
        start|stop|status)
            systemctl "$1" httpd ;;
        *)
            echo "usage: $0 {start|stop|status}" >&2
            exit 1 ;;
    esac
  4. Make them executable, check and trace.
    chmod +x /usr/local/bin/{mkusers,diskcheck,web}.sh
    for f in /usr/local/bin/{mkusers,diskcheck,web}.sh; do bash -n "$f" && echo "$f OK"; done
    bash -x /usr/local/bin/diskcheck.sh 50

ملاحظاتي

لا توجد ملاحظات بعد.

دروس هذا المعمل

الأوامر في هذا المعمل