Becoming a Linux Shell Scripting Ninja: Elevate Your DevOps User Management Game
Advance Linux Shell Scripting : Day 5 #90DaysofDevops
Table of contents
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 ๐ค
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.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.
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.
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.
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.