Advanced Linux Shell Scripting for DevOps Engineers with User Management

🚀 Aspiring DevOps & DevSecOps Engineer | Cloud DevOps (Azure & AWS) | Automation & Security Champion | Passionate Learner🌐
Hi, I’m Yuvraj! 🔥 I'm embarking on an exciting journey in the world of DevOps & DevSecOps, armed with hands-on expertise and a drive to excel. From automating tasks to building robust CI/CD pipelines, I’m dedicated to turning complex challenges into efficient and secure solutions.
What I Bring to the Table: ●Linux & Shell Scripting 🖥️ Automating processes and mastering command-line operations.
●Git & Git Branching 🔀 Seamless version control for effective collaboration.
●Build Tools 🔧 (Maven & npm) Crafting stable builds for quality software.
●CI/CD Tools 🚄 (Jenkins, GitHub Actions, Azure DevOps, GitLab CI/CD) Ensuring smooth & automated deployments.
●Security in DevOps 🛡️ Embedding security at every stage to safeguard development processes.
●Artifact Management 📦 (Nexus & Azure Artifacts) Managing software versions efficiently.
●Containerization & Orchestration 🐳⎈ (Docker & Kubernetes) Deploying scalable, agile applications.
●Infrastructure as Code (IaC) 📜 (Terraform & Ansible) Automating environment provisioning.
●Monitoring & Troubleshooting 🔍 Keeping systems in check with proactive issue resolution.
●Real-world & Corporate Projects 🌟 Hands-on experience with practical implementations.
✨ Always eager to learn, collaborate, and innovate! ✨ I’m ready to transform my academic knowledge and project experiences into real-world impact. Let’s connect and explore how we can drive meaningful change together! 🤝
Tasks
Create Directories Using Shell Script:
Write a bash script
createDirectories.shthat, when executed with three arguments (directory name, start number of directories, and end number of directories), creates a specified number of directories with a dynamic directory name.Example 1: When executed as
./createDirectories.shday 1 90, it creates 90 directories asday1 day2 day3 ... day90.Example 2: When executed as
./createDirectories.shMovie 20 50, it creates 31 directories asMovie20 Movie21 Movie22 ... Movie50.
#!/bin/bash
# Check if correct number of arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory_name> <start_number> <end_number>"
exit 1
fi
# Assigning arguments to variables
dir_name="$1"
start="$2"
end="$3"
# Loop to create directories
for (( i=start; i<=end; i++ )); do
mkdir "${dir_name}${i}"
done
Task 2: Create a Script to Backup All Your Work:
- Backups are an important part of a DevOps Engineer's day-to-day activities. The video in the references will help you understand how a DevOps Engineer takes backups (it can feel a bit difficult but keep trying, nothing is impossible).
#!/bin/bash
src_dir=/home/ubuntu/scripts
tgt_dir=/home/ubuntu/backups
curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
backup_file=$tgt_dir/$curr_timestamp.tgz
echo "Taking backup on $curr_timestamp"
tar czf $backup_file --absolute-names $src_dir
echo "Backup Complete"
What is Cron?
Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule jobs (commands or scripts) to run automatically at specified intervals. This could be anything from running a backup script every night at 2 AM, to sending out reminder emails every Monday morning, or cleaning up log files every hour.
Cron is started when the system boots up and runs in the background, checking the Crontab files to see if there are any jobs to execute.
What is Crontab?
Crontab (Cron table) is a configuration file that specifies shell commands to run periodically on a given schedule. Each user on a system can have their own Crontab file, allowing for personalized automation.
The Crontab file consists of a series of lines, each line representing a job, with the time and date fields followed by the command to be executed.
Crontab Syntax
A typical Crontab entry follows this format:
* * * * * command_to_execute
- - - - -
| | | | |
| | | | ----- Day of the week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
30 2 * * * /path/to/backup.sh
Editing Crontab
To edit your Crontab file, you use the crontab -e command. This opens your Crontab file in the default text editor, allowing you to add, modify, or remove jobs.
Copy codecrontab -e
To view your current Crontab entries, use:
Copy codecrontab -l
To remove all Crontab entries for the current user, use:
Copy codecrontab -r
Task 3: Read About Cron and Crontab to Automate the Backup Script:
- Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit, or delete entries to cron. A crontab file is a user file that holds the scheduling information.
Read About User Management:
A user is an entity in a Linux operating system that can manipulate files and perform several other operations. Each user is assigned an ID that is unique within the system. IDs 0 to 999 are assigned to system users, and local user IDs start from 1000 onwards.
Create 2 users and display their usernames.
Managing Users and Groups
Adding a User
To add a new user, use the useradd command:
sudo useradd -m username
- The
-moption creates a home directory for the user.
Removing a User
To remove a user, use the userdel command:
sudo userdel username


