2.6 Summarize scripting basics for server administration.
📘CompTIA Server+ (SK0-005)
Scripts are a series of commands that a computer executes automatically. To make them powerful, scripts use constructs—basic building blocks that control how the script behaves. The main constructs are variables, loops, conditionals, and comparators.
1. Variables
Definition:
A variable is a storage location in a script that holds data. Think of it as a named “container” for information that can change.
Why it’s used in IT:
- Store server names, IP addresses, or configuration settings.
- Store user input or results from a command to use later in the script.
Examples:
- In a PowerShell script, you can store a server name:
$ServerName = "Server01"
- In a Bash script, you can store an IP address:
IP="192.168.1.10"
Notes for the exam:
- Variables make scripts flexible. You don’t have to change the script everywhere; just update the variable.
- Variable names usually start with a letter and contain letters, numbers, or underscores.
2. Loops
Definition:
Loops let scripts repeat actions automatically, saving time and reducing manual effort.
Types of loops commonly used:
- For loop: Repeats a task a specific number of times.
- While loop: Repeats a task as long as a condition is true.
- ForEach loop: Repeats a task for each item in a list.
IT examples:
- Deploying updates to multiple servers:
$Servers = @("Server01","Server02","Server03")
foreach ($Server in $Servers) {
Restart-Computer -ComputerName $Server
}
This script restarts three servers automatically without doing each one manually.
- Checking log files continuously:
while [ -f /var/log/syslog ]; do
tail -n 10 /var/log/syslog
sleep 60
done
This monitors the last 10 lines of a log file every 60 seconds.
Notes for the exam:
- Loops reduce repetitive work.
- Infinite loops can happen if the exit condition is never met—scripts must have a stop condition.
3. Conditionals (If statements)
Definition:
Conditionals let the script make decisions based on certain conditions. They are “if this happens, do that; otherwise, do something else.”
IT examples:
- Check if a service is running:
if ((Get-Service "Spooler").Status -eq "Running") {
Write-Output "Print service is running"
} else {
Start-Service "Spooler"
Write-Output "Print service started"
}
- Checks if the Print Spooler service is running. If not, it starts it.
- Check free disk space on Linux:
if [ $(df / | tail -1 | awk '{print $5}' | sed 's/%//') -gt 80 ]; then
echo "Disk usage over 80%, cleanup required"
fi
Notes for the exam:
- Conditional statements are often paired with comparators.
- Common keywords:
if,else,elif(in Bash),elseif(in PowerShell).
4. Comparators
Definition:
Comparators are symbols or words that compare values. Scripts use them in conditionals and loops to make decisions.
Common Comparators:
| Type | Bash | PowerShell | Meaning |
|---|---|---|---|
| Equal to | -eq | -eq | Checks if values are equal |
| Not equal | -ne | -ne | Checks if values are not equal |
| Greater than | -gt | -gt | Checks if left value is greater |
| Less than | -lt | -lt | Checks if left value is smaller |
| Greater or equal | -ge | -ge | Checks if left value is greater or equal |
| Less or equal | -le | -le | Checks if left value is smaller or equal |
Example in IT:
- Check CPU load:
$CPUUsage = 75
if ($CPUUsage -gt 70) {
Write-Output "CPU is high, consider load balancing"
}
- Check if a server is online:
ping -c 1 192.168.1.10
if [ $? -eq 0 ]; then
echo "Server is online"
else
echo "Server is offline"
fi
Notes for the exam:
- Comparators are always used in conditionals or loops.
$?in Bash is a special variable that stores the exit status of the last command (0 = success).
✅ Key Takeaways for the Exam
- Variables store data for reuse and flexibility.
- Loops automate repetitive tasks (For, While, ForEach).
- Conditionals make decisions based on conditions (
if,else). - Comparators allow scripts to evaluate data (
-eq,-ne,-gt,-lt). - These constructs often work together. For example, a loop may use a conditional to act only on certain items.
Tip: Most exam questions may ask you to identify what a script does, choose the right construct, or predict the output of a script snippet.
