cancel
Showing results for 
Search instead for 
Did you mean: 
TudorRaduta
Community Manager
Community Manager
  • 79 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.

0 Kudos
1 Reply
SANJURAJ
Mission Specialist
Mission Specialist
  • 27 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
Join the discussion
You must log in to join this conversation.