1.7 KiB
1.7 KiB
Linux+ 4.2.2: Conditions and tests
Numeric tests with exit codes, file and string tests, a regular
expression match and a case statement.
Before you start
Work as root, so ~ is /root. The lab only checks that
~/diskcheck.sh doesn't exist yet. Write answers to the files in
/root/lab-answers/.
Tasks
- Write the executable script
~/diskcheck.sh. It takes a limit in percent as its first argument (default 80) and reads how full/is. At or above the limit it printsALERT: / is N%and exits 2; at or above the limit minus 10 it printswarn: / is N%and exits 1; otherwise it printsok: / is N%(exit 0). Run it with no argument and with5. - Loop over
/etc/passwd,/etc,/nopeand/bin, and print for each whether it is alink,dir,fileormissing(asPATH: TYPE, testing for a link first). Then test an empty variable with-z(printingempty), and compare10and9as strings with[[ 10 > 9 ]] || echo "string compare says 10 < 9". Save all of the output in/root/lab-answers/2. - For
10.0.5.21,10.0.5andweb01, printX: ipv4orX: not ipv4using a regular expression match. Then, forstart,stopandbogus, use acasestatement to printX okfor start and stop andX unknownfor anything else. Save all of the output in/root/lab-answers/3.
Hints
df --output=pcent / | tail -1 | tr -dc '0-9',[ "$used" -ge "$limit" ],$((limit - 10)),exit 2.[ -L ],[ -d ],[ -f ];{ ...; } > /root/lab-answers/2saves the output of several commands.[[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]];case $a in start|stop) ...;; *) ...;; esac.