Process output of shell commands within a script

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:

  1. Command Substitution
  2. 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:

  • who lists logged-in users.
  • wc -l counts lines (each line is a user).
  • $(who | wc -l) stores the number in user_count.
  • Then echo prints 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 patterns
  • awk → process and print columns
  • cut → extract fields
  • sort → sort lines
  • uniq → 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 -h shows 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 aux lists all running processes.
  • --sort=-%mem sorts by memory usage (highest first).
  • head -5 shows 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.txt reads the file with service names.
  • Loop processes each service individually.
  • systemctl restart restarts 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 of who into /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:

TaskHow 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 filestar -czf backup.tar.gz $(find /data -type f -name "*.log")

6. Exam Tips

  1. Know how to use command substitution: $(command) is preferred.
  2. Understand piping and filtering: you may need to process command output using grep, awk, cut, etc.
  3. Loops and file input/output: know how to read output line by line.
  4. Redirection: know >, >>, <.
  5. 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.

Buy Me a Coffee