Becoming a Linux Shell Scripting Ninja: Elevate Your DevOps User Management Game

Advance Linux Shell Scripting : Day 5 #90DaysofDevops

ยท

4 min read

Table of contents

No heading

No headings in the article.

My Journey to Becoming a Linux Shell Scripting Ninja ๐Ÿง

๐Ÿš€ Introduction ๐Ÿš€

Hey, fellow tech enthusiasts! I can't wait to share my exciting journey with you all. Imagine becoming a Linux Shell Scripting Ninja ๐Ÿฑโ€๐Ÿ’ป, elevating your DevOps user management game, and sharing your knowledge with popular Hashnode bloggers. Yes, it's all possible, and I'm going to show you how.

๐Ÿ’ป Task: Advanced Linux Shell Scripting for DevOps Engineers with User Management ๐Ÿ’ป

As a passionate DevOps Engineer, I wanted to level up my skills in Linux Shell Scripting. With so many tasks to handle daily, I knew mastering shell scripting would streamline my workflow and make me more efficient.

๐Ÿ“ The Discovery ๐Ÿ“

One day, while exploring a repository, I came across an intriguing challenge. There were a total of 90 subdirectories in the '2023' directory, each representing a day. I wondered how these directories were created. Did someone painstakingly create them manually? Or was there a smarter way to achieve this?

๐Ÿ” The Revelation ๐Ÿ”

My curiosity got the best of me, and after some research, I found the answer โ€“ it was done using a simple shell command! To create 90 directories in seconds, all it took was the following command:

mkdir day{1..90}

Mind-blown! This was a game-changer, and I knew I had to learn more about shell scripting to leverage its power for my DevOps tasks.

๐Ÿš€ The Bash Script ๐Ÿš€

I decided to write a bash script named createDirectories.sh that could create a dynamic number of directories with a given name, starting from a specified number and ending with another. I aimed to make it flexible and efficient.

๐Ÿ“œ The Script ๐Ÿ“œ

#!/bin/bash

# Usage: ./createDirectories.sh <directory_name> <start_number> <end_number>

if [ "$#" -ne 3 ]; then
  echo "Usage: ./createDirectories.sh <directory_name> <start_number> <end_number>"
  exit 1
fi

dir_name="$1"
start_num="$2"
end_num="$3"

for ((i=start_num; i<=end_num; i++)); do
  mkdir "${dir_name}${i}"
done

๐Ÿ“ Usage ๐Ÿ“

To use the script, simply execute it with three arguments: the desired directory_name, the start_number, and the end_number. For instance:

./createDirectories.sh day 1 90

This command will create 90 directories, named day1, day2, day3, and so on, up to day90. You can apply the same logic to create any number of directories for different purposes!

๐Ÿ“‚ Backup Your Work ๐Ÿ“‚

As a responsible DevOps Engineer, I knew the importance of backing up my work regularly. Automation was the key, and I discovered Cron and Crontab as powerful tools for this task.

๐Ÿ•ฐ๏ธ The Cron Job ๐Ÿ•ฐ๏ธ

Cron is like having a personal assistant that schedules tasks for you. By using Crontab, I could set up a backup script to run automatically at specified intervals.

โณ The Backup Script โณ

#!/bin/bash

# Replace '/path/to/backup' with the actual directory path you want to backup to.

backup_dir="/path/to/backup"

# Backup specific files or directories, like databases, configuration files, or project folders.

cp -R /path/to/source_directory $backup_dir

# Add more backup commands as needed.

# Make sure the script is executable by running 'chmod +x backupScript.sh'.

With this script in place, I could sit back and relax, knowing that my hard work and data were safely backed up.

๐Ÿ“š The Learning Journey ๐Ÿ“š

Embracing Linux Shell Scripting and automating tasks felt empowering. I began exploring user management and the commands that enable me to control and manipulate user accounts on my system.

๐Ÿ” The User Management ๐Ÿ”

In a Linux operating system, users play a vital role in file manipulation and other operations. Each user has a unique ID, and as I delved deeper into user management, I realized its significance in system administration.

๐Ÿ‘ฉโ€๐Ÿ’ป My Personal Experience ๐Ÿ‘จโ€๐Ÿ’ป

Creating and managing users became a fascinating adventure. I decided to try it firsthand and discovered two fantastic commands โ€“ useradd and id.

๐Ÿ‘ค The First User ๐Ÿ‘ค

useradd -m user1
id user1

๐Ÿ‘ค The Second User ๐Ÿ‘ค

useradd -m user2
id user2

๐Ÿ‘€ Conclusion ๐Ÿ‘€

Becoming a Linux Shell Scripting Ninja was an incredible journey that opened up new possibilities in my DevOps career. Embracing automation, backups, and user management elevated my skills to the next level.

๐Ÿค” FAQs ๐Ÿค”

  1. Can I run the createDirectories.sh script on Windows? The script is designed for Unix-like systems, but you can use Windows Subsystem for Linux (WSL) to run it on Windows.

  2. How often should I schedule my backup script using Cron? The frequency of backups depends on the importance of your data. For critical data, daily backups are recommended.

  3. Are Cron and Crontab available on all operating systems? Cron is commonly found on Unix-like systems, but some variations exist. Crontab is available on most Unix-based systems.

  4. Can I automate other tasks using Cron? Absolutely! Cron is versatile and allows you to automate various tasks like system maintenance, log rotation, and more.

  5. What's the significance of user IDs starting from 1000 in Linux? User IDs 1 to 999 are typically reserved for system users, while user IDs 1000 and above are used for regular users.

ย