Skip to content
CS-505 · Linux (LAB)/Quick Revision Short Notes

Linux (LAB) (CS-505) - Unit 2 Short Notes

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.

  1. A value is read with a dollar sign, so echo $name prints it, and all values are strings by default.
  2. A shell variable is local to the current shell, while export name turns it into an environment variable that child processes inherit.
  3. Common environment variables are PATH, HOME, USER, SHELL and PWD, and set or env lists them.
  4. unset name removes a variable, and read name fills 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.

  1. The syntax is if [ condition ]; then ... elif ... else ... fi, and fi closes the block.
  2. Conditions use test or [ ], with numeric operators -eq -ne -lt -gt and string operators = !=, and spaces inside the brackets are compulsory.
  3. case $x in pattern) commands ;; esac selects one branch out of many by pattern matching.
  4. 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.

  1. for i in 1 2 3; do echo $i; done runs once for each item in a list.
  2. while [ cond ]; do ... done repeats as long as the condition is true.
  3. until [ cond ]; do ... done repeats until the condition becomes true.
  4. Arithmetic is done with i=$((i+1)), and done closes 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.

  1. break transfers control to the first command after done, and break n leaves n nested loops.
  2. continue jumps back to the loop test, so the remaining commands of that pass are skipped.
  3. Both are normally placed inside an if so they run only when a condition holds.
  4. They work in for, while and until loops.

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.

  1. The suffixes s, m, h and d give seconds, minutes, hours and days, so sleep 2m waits two minutes.
  2. It uses almost no CPU while waiting.
  3. It is used inside loops to poll or retry, and to delay a script, for example while true; do date; sleep 5; done.
  4. 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.

  1. bash -x script.sh prints each command, after expansion, with a + prefix before it runs.
  2. bash -v script.sh prints each line as it is read, before expansion.
  3. set -x and set +x inside the script switch tracing on and off for one section.
  4. bash -n script.sh only checks syntax without running the script, and echo statements 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.

  1. Web server: Apache (httpd) or Nginx serves web pages over HTTP.
  2. File server: Samba shares files and printers with Windows clients, and NFS shares them with Unix clients.
  3. Directory server: OpenLDAP keeps central user and resource records. Application server: Tomcat runs Java web applications.
  4. DNS server: BIND resolves names to IP addresses. SMTP server: Postfix or Sendmail sends and routes mail.
  5. 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=5 with no spaces, and read it as $x.
  • export makes a variable visible to child processes.
  • An if block ends with fi, a case block with esac, and a loop with done.
  • -eq -ne -lt -gt compare numbers, while = != compare strings.
  • $? holds the exit status of the last command, and 0 means success.
  • break exits the loop, and continue skips to the next iteration.
  • sleep 5 pauses for 5 seconds.
  • bash -x traces, bash -v echoes lines, and bash -n checks 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.
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in