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
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.