Use looping constructs (for, etc.) to process input

3. Create Simple Shell Scripts

πŸ“˜Red Hat Certified System Administrator (RHCSA – EX200)


This topic is very important for the RHCSA exam. You must understand how to use loops inside Bash shell scripts to process multiple inputs automatically.

In system administration, loops are commonly used to:

  • Process multiple files
  • Manage multiple users
  • Check multiple services
  • Apply changes to many systems or directories
  • Read input line by line from a file

A loop allows you to repeat commands multiple times without writing the same command again and again.


What Is a Loop?

A loop is a control structure in a shell script that repeats a block of commands until a condition is met.

In Bash (the default shell in Red Hat systems), the most commonly used loops are:

  1. for loop
  2. while loop
  3. until loop

For the RHCSA exam, you must especially understand:

  • for loops
  • while loops
  • How to process input (files, command output, arguments)

1. The for Loop

The for loop is used when you want to repeat commands for a list of items.

Basic Syntax

for variable in list
do
commands
done

How It Works:

  • The loop assigns each value from the list to the variable.
  • It runs the commands once for each value.

Example 1: Processing Multiple Files

Suppose you want to change permissions on multiple log files:

for file in /var/log/*.log
do
chmod 600 "$file"
done

What happens:

  • /var/log/*.log generates a list of log files.
  • Each filename is assigned to file.
  • chmod runs for each file.

This is very common in IT environments when managing logs.


Example 2: Loop Through Usernames

for user in user1 user2 user3
do
useradd "$user"
done

This will create three users.

In real IT tasks, you may use this to automate user creation.


Example 3: Loop Using Command Output

You can loop over command output:

for user in $(cat users.txt)
do
useradd "$user"
done

Here:

  • cat users.txt reads usernames from a file.
  • Each word becomes one loop item.

⚠ Important:
This works only if usernames are separated by spaces or newlines and contain no spaces.


Example 4: Numeric for Loop

You can loop through numbers:

for i in {1..5}
do
echo "Backup number $i"
done

This runs 5 times.

You can also use:

for (( i=1; i<=5; i++ ))
do
echo "$i"
done

This is similar to C-style looping.


2. The while Loop

The while loop runs as long as a condition is true.

Basic Syntax

while condition
do
commands
done

Example 1: Reading a File Line by Line (Very Important for Exam)

This is one of the most important patterns:

while read line
do
echo "User: $line"
done < users.txt

Explanation:

  • read line reads one line at a time.
  • The loop continues until the file ends.
  • < users.txt sends the file into the loop.

This method is safer than using for $(cat file).

In IT environments, this is used to:

  • Create users from a file
  • Modify configurations
  • Process IP address lists
  • Process hostnames

Example 2: Create Users from File

while read username
do
useradd "$username"
done < users.txt

Each line in users.txt becomes one username.


Example 3: Loop While a Service Is Active

while systemctl is-active httpd >/dev/null 2>&1
do
echo "Service is running"
sleep 5
done

This checks repeatedly while the service is active.


3. The until Loop

The until loop runs until a condition becomes true.

Syntax

until condition
do
commands
done

It is the opposite of while.

Example:

until ping -c1 server1 >/dev/null 2>&1
do
echo "Waiting for server..."
sleep 5
done

The loop runs until the server responds.


Processing Input in Loops

For RHCSA, β€œprocessing input” usually means:

  1. Reading input from:
    • Files
    • Command output
    • User input
    • Script arguments
  2. Performing actions on each input item.

1. Processing Script Arguments

Example script:

#!/bin/bashfor user in "$@"
do
echo "Creating user: $user"
useradd "$user"
done

If you run:

./script.sh user1 user2 user3

"$@" represents all arguments.

This is commonly tested in exams.


2. Processing Command Output Safely

Better method using while read:

ls /home | while read dir
do
echo "Directory: $dir"
done

This processes each directory name.


3. Using IFS (Input Field Separator)

By default, Bash splits input on:

  • Space
  • Tab
  • Newline

Sometimes you must control splitting:

while IFS=: read user pass uid gid comment home shell
do
echo "User: $user"
done < /etc/passwd

Here:

  • IFS=: tells Bash to split fields using :.
  • Useful for processing structured files.

This is very important for system administrators.


Common Mistakes to Avoid (Important for Exam)

  1. ❌ Forgetting do
  2. ❌ Forgetting done
  3. ❌ Not quoting variables ("$file")
  4. ❌ Using for $(cat file) with complex input
  5. ❌ Infinite loops without condition change

Loop Control Commands

break

Stops the loop completely.

break

continue

Skips the current iteration.

continue

These are useful in scripts where you need conditional control.


Best Practices for RHCSA

βœ” Always use #!/bin/bash at the top of scripts
βœ” Make scripts executable:

chmod +x script.sh

βœ” Test scripts carefully
βœ” Use while read for file processing
βœ” Quote variables to avoid errors
βœ” Understand redirection (< file)
βœ” Know how to process arguments using "$@"


What the RHCSA Exam May Test

You may be asked to:

  • Write a script that loops through a list of users and creates them
  • Read a file and perform operations on each line
  • Change permissions for multiple files
  • Process directories
  • Use numeric loops
  • Use break or continue
  • Use loops with conditional statements (if inside loop)

Example exam-style task:

“Create a script that reads usernames from a file and creates each user only if it does not already exist.”

Solution concept:

while read user
do
if ! id "$user" &>/dev/null
then
useradd "$user"
fi
done < users.txt

This combines:

  • while
  • read
  • if
  • command checking
  • input processing

Summary

For RHCSA 3.2, you must understand:

  • How for loops work
  • How while loops work
  • How until loops work
  • How to read input from files
  • How to process command output
  • How to process script arguments
  • How to safely quote variables
  • How to use break and continue

Loops are extremely important in Linux system administration because they allow automation of repetitive administrative tasks.

If you understand how to:

  • Read input
  • Loop through it
  • Apply commands correctly

You will be fully prepared for this section of the RHCSA exam.

Buy Me a Coffee