Happy Monday, everyone, and welcome back! Let's kick off the week with a new hands-on mission.
We have been mastering individual commands. Now it is time to put them together. Our mission this week is to tackle the RHCSA objective "Create simple shell scripts" including handling inputs, using conditionals, and creating loops.
Our mission is to learn how to write a simple, smart, and safe BASH script. We will practice making it executable, checking user input (like $1 and $#), and using a for loop to automate a repetitive task.
When you are writing a script in the exam, your best friends are the man pages:
man bash (search for "SHELL GRAMMAR" to find loops)man test (the key to your if statements)The Scenario: You need to write a script named create_users.sh that reads a list of usernames from a file and creates a local user account for each one.
Before we write the full script, let's master the checks. How would you write just the conditional if block that verifies:
Usage: ./create_users.sh <filename> and exits with an error.-f)Now let us see the entire script. It should do everything from Part 1 and also:
for loop or a while read loop to read every line from the input file.useradd command to create the new user.Created user: [username] for each account.$EUID variable)Let us see those scripts in the comments.
Greetings All. Please check the below script which may serve the purpose. Note: For all the operating users to access the script, I have placed the script under /usr/local/bin directory. I have used the user mike as the regular user to test the script.The script
Current set of users
Testing it as a regular user mike
Performing the user creation as root
Confirming the user exists in the system
hints for learners who are trying to master Bash scripting :
Step 1: Checking for the Filename Argument
Before diving into any logic, your script should make sure it’s being used correctly. The user should give exactly one argument i.e. the name of the file with the user list.
Here is how you can do it:
if [ $# -ne 1 ]; then echo "Usage: ./create_users.sh <filename>" exit 1 fi
"$#" counts how many arguments were passed to the script.
The -ne operator means "not equal." This check ensures the script only continues if there’s exactly one argument.
If the argument count isn’t one, the script prints a helpful usage message and exits with an error.
Step 2: Verifying the Argument Is a File
Once you know the user gave you an argument, you need to be sure it points to a real file. This prevents frustrating errors if the file doesn’t exist.
Add this check immediately after the previous one:
if [ ! -f "$1" ]; then echo "Error: File '$1' not found." exit 1 fi
"$1" refers to the first argument (which should be the filename).
[ ! -f "$1" ] checks that this argument is not a regular file.
If the file doesn’t exist, the script displays a clear error message and quits.
Step 3: Reading the File and Adding Users
Now it’s time for the real action: reading the file contents and creating users based on each line (each line should be a username).
The most robust and beginner-friendly way to do this is with a while read loop:
while read -r username; do useradd "$username" echo "Created user: $username" done < "$1"
This loop goes through every line in the file, treating each as a new username.
For each username, the script runs useradd to create the user, then prints a message confirming the action.
Alternative (Not Recommended) method is : for Loop
for username in $(cat "$1"); do useradd "$username" echo "Created user: $username" done
This works if your file is simple, but it can break on usernames with spaces or special characters.
Best Practice: Stick to the while read loop for consistent, safe results.
Step 4: Bonus – Only Allow Root to Run the Script
System changes (like adding users) should only be done by the system administrator , the root user. You can ensure your script enforces this with one more check, added right at the top:
if [ "$EUID" -ne 0 ]; then echo "This script must be run as root." exit 1 fi
$EUID tells you who’s running the script: root has an ID of 0.
If it’s anything else, the script simply stops and explains why.
https://www.gnu.org/software/bash/manual/bash.html
https://www.baeldung.com/linux/shell-script-iterate-over-string-list
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.