How unit 2 is examined
This unit covers shell variables, if and loop control, break and continue, sleep, script debugging and Linux server roles. No past question has been asked on any topic, so every topic is short but complete.
Shell Variables
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. ==A shell variable is a named memory location, created as name=value with no spaces around =, that stores a string the shell and its scripts can reuse.==
Key points.
- A value is read with a dollar sign, so
echo $nameprints it, and all values are strings by default. - A shell variable is local to the current shell, while
export nameturns it into an environment variable that child processes inherit. - Common environment variables are
PATH,HOME,USER,SHELLandPWD, andsetorenvlists them. unset nameremoves a variable, andread namefills it from the keyboard.
Branching Control Structures
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>A branching structure lets a script choose which commands to run by testing a condition, using if, elif, else and case.</mark>
Key points.
- The syntax is
if [ condition ]; then ... elif ... else ... fi, andficloses the block. - Conditions use
testor[ ], with numeric operators-eq -ne -lt -gtand string operators= !=, and spaces inside the brackets are compulsory. case $x in pattern) commands ;; esacselects one branch out of many by pattern matching.- The condition succeeds when the command's exit status
$?is 0.
if [ $n -gt 0 ]; then echo positive; else echo not positive; fi
Loop-Control Structure
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>A loop repeats a block of commands, using for, while or until.</mark>
Key points.
for i in 1 2 3; do echo $i; doneruns once for each item in a list.while [ cond ]; do ... donerepeats as long as the condition is true.until [ cond ]; do ... donerepeats until the condition becomes true.- Arithmetic is done with
i=$((i+1)), anddonecloses every loop.
Continue and break Statements
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>break leaves the loop immediately, and continue skips the rest of the current iteration and starts the next one.</mark>
Key points.
breaktransfers control to the first command afterdone, andbreak nleaves n nested loops.continuejumps back to the loop test, so the remaining commands of that pass are skipped.- Both are normally placed inside an
ifso they run only when a condition holds. - They work in
for,whileanduntilloops.
Sleep Command
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>sleep N pauses the shell or script for N seconds and then continues with the next command.</mark>
Key points.
- The suffixes
s,m,handdgive seconds, minutes, hours and days, sosleep 2mwaits two minutes. - It uses almost no CPU while waiting.
- It is used inside loops to poll or retry, and to delay a script, for example
while true; do date; sleep 5; done. sleep 10 &runs the pause in the background.
Debugging Script
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Debugging a shell script means tracing its execution to find errors, mainly with bash -x script.sh.</mark>
Key points.
bash -x script.shprints each command, after expansion, with a+prefix before it runs.bash -v script.shprints each line as it is read, before expansion.set -xandset +xinside the script switch tracing on and off for one section.bash -n script.shonly checks syntax without running the script, andechostatements help inspect values.
Use of Linux as web-server, file server, directory server, application server, DNS server, SMTP server, Firewall, Proxy server
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Linux is a stable, free and secure operating system that can be configured to run many network services on one machine.</mark>
Key points.
- Web server: Apache (httpd) or Nginx serves web pages over HTTP.
- File server: Samba shares files and printers with Windows clients, and NFS shares them with Unix clients.
- Directory server: OpenLDAP keeps central user and resource records. Application server: Tomcat runs Java web applications.
- DNS server: BIND resolves names to IP addresses. SMTP server: Postfix or Sendmail sends and routes mail.
- Firewall: iptables or nftables filters packets by rule. Proxy server: Squid caches web pages and controls client access.
Last-minute revision
- Set a variable as
x=5with no spaces, and read it as$x. exportmakes a variable visible to child processes.- An
ifblock ends withfi, acaseblock withesac, and a loop withdone. -eq -ne -lt -gtcompare numbers, while= !=compare strings.$?holds the exit status of the last command, and 0 means success.breakexits the loop, andcontinueskips to the next iteration.sleep 5pauses for 5 seconds.bash -xtraces,bash -vechoes lines, andbash -nchecks syntax.- Apache is the web server, Samba the file server, BIND the DNS server, Postfix the SMTP server, Squid the proxy and iptables the firewall.
Memory hooks
- Closers: if to fi, case to esac, do to done.
- Break to Bail out, Continue to Carry on.
- x for eXecution trace, v for Verbose, n for No run.
- ABC-SPS: Apache, BIND, Samba, Postfix, Squid, plus iptables.
Coverage checklist
- Shell Variables: no past questions.
- Branching Control Structures: no past questions.
- Loop-Control Structure: no past questions.
- Continue and break Statements: no past questions.
- Sleep Command: no past questions.
- Debugging Script: no past questions.
- Use of Linux as web-server, file server, directory server, application server, DNS server, SMTP server, Firewall, Proxy server: no past questions.