3. Create Simple Shell Scripts
📘Red Hat Certified System Administrator (RHCSA – EX200)
In Linux, shell scripts are used to automate tasks. Often, you want your script to use the output of a command—like ls, df, ps, or grep—and do something with it. Processing output of commands is essential for system administration.
There are two main ways to process command output in a script:
- Command Substitution
- Pipes (
|) and Redirection (>,>>)
Let’s go step by step.
1. Command Substitution
Command substitution lets you capture the output of a command into a variable and use it later in your script.
Syntax:
variable=$(command)
or
variable=`command` # old style, still works
Example in IT environment:
Suppose you want to check the number of active users on the system and store it:
#!/bin/bash# Count the number of logged-in users
user_count=$(who | wc -l)echo "There are $user_count users currently logged in."
Explanation:
wholists logged-in users.wc -lcounts lines (each line is a user).$(who | wc -l)stores the number inuser_count.- Then
echoprints it.
✅ Key Point for Exam: You need to know how to use $(command) to save output to a variable.
2. Using Pipes and Filters
Sometimes, you want to process command output directly without storing it in a variable. For that, you use pipes (|) to send the output of one command to another.
Common filters:
grep→ search for patternsawk→ process and print columnscut→ extract fieldssort→ sort linesuniq→ remove duplicates
Example 1: Check disk usage and alert if any partition is above 80%:
#!/bin/bashdf -h | awk '$5+0 > 80 {print "Warning: Partition", $1, "is", $5, "full"}'
Explanation:
df -hshows disk usage.awk '$5+0 > 80 {print ...}'processes the 5th column (usage percentage) and prints a warning if >80%.- No variables needed—output is processed directly.
Example 2: Find processes using most memory:
#!/bin/bashps aux --sort=-%mem | head -5
ps auxlists all running processes.--sort=-%memsorts by memory usage (highest first).head -5shows top 5 memory-consuming processes.
3. Loops to Process Command Output
Often, the output has multiple lines, and you want to process each line separately.
Example: Restart services listed in a file
#!/bin/bash# List of services to restart
services=$(cat services.txt)for svc in $services
do
systemctl restart $svc
echo "Restarted $svc"
done
Explanation:
cat services.txtreads the file with service names.- Loop processes each service individually.
systemctl restartrestarts each service.
4. Redirection
>→ overwrite a file>>→ append to a file<→ read from a file
Example: Save current logged-in users to a file
#!/bin/bashwho > /tmp/current_users.txt
>writes the output ofwhointo/tmp/current_users.txt.- You can later read or process this file in scripts.
5. Practical IT Use Cases
Here are some examples of how processing command output is used in real IT tasks:
| Task | How it’s done in scripts |
|---|---|
| Monitor disk usage | `df -h |
| Check CPU load | `top -b -n1 |
| Count logged-in users | `who |
| Restart failed services | `systemctl list-units –failed |
| Backup files | tar -czf backup.tar.gz $(find /data -type f -name "*.log") |
6. Exam Tips
- Know how to use command substitution:
$(command)is preferred. - Understand piping and filtering: you may need to process command output using
grep,awk,cut, etc. - Loops and file input/output: know how to read output line by line.
- Redirection: know
>,>>,<. - Practice examples: RHCSA often tests scripts that generate reports or handle services/files using command output.
✅ Summary
- Command substitution: Save command output in a variable →
var=$(command) - Pipes: Send output from one command to another →
command1 | command2 - Loops: Process multiple lines of output →
for line in $(command) - Redirection: Save or read output from files →
>,>>,< - IT examples: Monitor disk, memory, services, users, and automate administrative tasks.
By mastering these, you can write scripts that react to system data, which is exactly what RHCSA expects.
