Skip to main content

Command Palette

Search for a command to run...

Day 5:🚀Mastering Linux Shell Scripting: Empowering DevOps Engineers with User Management Skills👥💻🔧

Day#5 of the #90DaysOfDevOps Challenge!

Published
5 min read
Day 5:🚀Mastering Linux Shell Scripting: Empowering DevOps Engineers with User Management Skills👥💻🔧
V

Experienced QA professional with a passion for manual and automation testing. Proficient in DevOps practices, ensuring seamless integration and continuous delivery. Dedicated to ensuring top-notch software quality and efficiency. Eager to contribute my skills to Hashnode's vibrant tech community. Let's collaborate and create exceptional experiences!

🙌Introduction

Welcome to Day 5 of the #90DaysOfDevOps challenge!

Today, we will cover several topics related to Linux system administration and shell scripting. We'll start with creating dynamic directories using the command line, followed by an automated backup script, and then we'll learn how to schedule the backup script with the help of Cron. Additionally, we'll explore user management in Linux, including creating and displaying usernames. Finally, we'll conclude our blog with a script to create multiple directories with dynamic names based on user input.

📁 Creating Dynamic Directories

Before we dive into the script, let's first understand how to create dynamic directories using a simple command in Linux: mkdir day{1..N}

The above command will create N directories named day1, day2, day3, and so on, up to dayN. This demonstrates a quick and efficient way to create multiple directories with sequential names.

📁Creating Dynamic Directories using Shell Scripting

Let's now develop a bash script that allows users to create multiple directories with dynamic names based on their inputs. The script will use either loops or command-line arguments to achieve this.

Create a new file named createDirectories.sh and open it in a text editor vim

Explanation of the Script:

The script starts by checking if all three arguments (directory name, start number, and end number) are provided.

If arguments are not provided then, it will display a usage message and exit.

It then assigns the provided arguments to variables directory_name, start_number, and end_number. Using a for loop, the script iterates through the range from start_number to end_number, creating directories with names formed by appending the current number to the directory_name.

Now, Make the script executable & Now, users can run the script with three arguments: the desired directory name and the range of directories they want to create.

👤 User Management in Linux

User management is an integral part of Linux system administration. It involves creating, modifying, and managing user accounts on the system. Let's explore how to create a new user account and display a list of all existing users.

To create a new user in Linux, use the useradd command.

Note: To create new users, you will need root or administrative privileges on the system.

Step 1: Create the first user To create a user named "UserOne,"

Step 2: Create the second user To create a user named "UserTwo,"

Step 3: Display the usernames In Linux - user information is typically stored in the /etc/passwd file. Each line in this file represents a user account and contains various fields separated by colons.

To display the usernames of the two users you just created, you can simply use the cut command to extract the first field (username) from the /etc/passwd file: This command will display the usernames of all users whose names start with "user." You should see the output as:

Another command you can use to display the usernames in Linux is the awk command. awk is a powerful text processing tool that allows you to manipulate data and extract specific fields easily. To display the usernames using awk, you can do the following:

The -F: option sets the field separator to the colon (:) since the /etc/passwd file uses colons to separate fields.

{print $1} instructs awk to print the first field, which is the username. /etc/passwd is the input file containing user account information.

grep '^user' is used to filter and display only the usernames starting with "user."

You can replace 'user' with any desired username prefix or remove grep altogether to display all usernames.

After running this command, you should see the same output as before:

Both the cut and awk commands are effective ways to display usernames in Linux, and you can choose the one that suits your preferences or the task at hand.

💾 Automated Backup Script

Having a reliable backup solution is crucial for safeguarding important files and data from accidental loss.

To create a script that backs up all your work done till now in Linux and creates an archive, we'll use the tar command, which is commonly used for archiving files. So,Follow the steps below to create the backup script:

Step 1: Create the Backup Script Create a new file named backup_work.sh and open it in a text editor.

Step 2: Add the Script Content Add the following content to the backup_work.sh script:

Step 3: Make the Script Executable Make the script executable with the following command:

Step 4: Run the Backup Script Now

The script will create a backup archive with the current date and time in the filename and store it in the specified backup directory. All the directories listed in the source_dirs array will be included in the backup archive.

For example, if you run the script on July 25, 2023, at 15:30:00, the archive filename would look like work_backup_2023-07-25_15-30-00.tar.gz.

Certainly! By scheduling this script to run automatically using Cron, you can ensure that your work is regularly backed up. This approach guarantees the safety and recoverability of your data in the event of any potential data loss. Now, let's proceed to the next point.

🕐 Automating the Backup Script with Cron

Automation is a boon for system administrators, and Cron provides an excellent way to schedule tasks to run at specified intervals. To automate our backup script, we'll schedule it to run at a designated time using Cron.

To edit the Cron table, run the following command:

Add the following line to the Cron table to schedule the backup script to run daily at 3:00 AM:

The five * shows the schedule: minute, hour, day of the month, month, and day of the week, respectively. 0 3 *: This schedule means the script will run at 3:00 AM daily.

🎉 Conclusion 🎉

In this comprehensive blog post, we covered various topics related to Linux system administration and shell scripting. We learned how to create dynamic directories using a simple command, create an automated backup script to protect our data and schedule it using Cron for automated execution. Additionally, we explored the fundamental aspects of user management in Linux, including creating and displaying usernames.

We also developed a powerful bash script that enables users to create multiple directories with dynamic names based on their inputs. This script demonstrates the versatility of shell scripting and how it can be used to streamline repetitive tasks and enhance productivity.

Happy scripting!