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:
forloopwhileloopuntilloop
For the RHCSA exam, you must especially understand:
forloopswhileloops- 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/*.loggenerates a list of log files.- Each filename is assigned to
file. chmodruns 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.txtreads 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 linereads one line at a time.- The loop continues until the file ends.
< users.txtsends 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:
- Reading input from:
- Files
- Command output
- User input
- Script arguments
- 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)
- β Forgetting
do - β Forgetting
done - β Not quoting variables (
"$file") - β Using
for $(cat file)with complex input - β 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
breakorcontinue - Use loops with conditional statements (
ifinside 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:
whilereadif- command checking
- input processing
Summary
For RHCSA 3.2, you must understand:
- How
forloops work - How
whileloops work - How
untilloops 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
breakandcontinue
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.
