هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
الهدف
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/.
المهام
ضع علامة على المهمة عندما تنتهي منها. متصفحك فقط يتذكر ذلك.
- 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. - 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). - Script
web.sh: with the argumentstart,stoporstatusit does that with httpd; with anything else it prints a usage line and exits with 1. - 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 1prints a WARNING.
تلميحات
اعرض التلميحات
[ $# -lt 1 ],while read -r NAME,id "$NAME" &>/dev/null.df --output=pcent / | tail -1 | tr -dc '0-9'gives the number.case "$1" in start|stop|status) … ;; *) … ;; esac.bash -n FILE,bash -x FILE.
الحل
اعرض الحل
- 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" - 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 - 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 - Make them executable, check and trace.
ملاحظاتي
لا توجد ملاحظات بعد.