cancel
Showing results for 
Search instead for 
Did you mean: 
TudorRaduta
Community Manager
Community Manager
  • 258 Views

Monday Challenge: The Scripting Lab (Easy & Hard Mode)

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.

The Mission

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.

The Map

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)

Your Challenge

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.

Part 1: The Beginner Challenge (Input and Conditionals)

Before we write the full script, let's master the checks. How would you write just the conditional if block that verifies:

  1. The script checks that exactly one argument (a filename) was provided.
  2. If not, it prints an error message such as Usage: ./create_users.sh <filename> and exits with an error.
  3. The script also checks if the provided filename exists and is a regular file. (Hint: use -f)

Part 2: The Expert Challenge (The Full Script)

Now let us see the entire script. It should do everything from Part 1 and also:

  1. Use a for loop or a while read loop to read every line from the input file.
  2. For each line (username) in the file, run the useradd command to create the new user.
  3. Print a message such as Created user: [username] for each account.
  • Bonus Question: How would you make sure this script can only be run by the root user? (Hint: check the $EUID variable)

Let us see those scripts in the comments.

2 Replies
SANJURAJ
Mission Specialist
Mission Specialist
  • 206 Views

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 scriptThe scriptCurrent set of usersCurrent set of usersTesting it as a regular user mikeTesting it as a regular user mikePerforming the user creation as rootPerforming the user creation as rootConfirming the user exists in the systemConfirming the user exists in the system

0 Kudos
Chetan_Tiwary_
Community Manager
Community Manager
  • 126 Views

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: BonusOnly 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 

0 Kudos
Join the discussion
You must log in to join this conversation.