Advanced Linux Shell Scripting for DevOps Engineers with User management

🔹Introduction

Shell scripting is a powerful skill for automating tasks in a Unix-like environment. With shell scripts, you can combine commands, loops, conditionals, and other programming constructs to perform complex operations efficiently. It is an essential skill for DevOps engineers, system administrators, and anyone working in a Unix-based operating system.

🔹Create Directories with Dynamic Names

Create a bash script, createDirectories.sh, that takes three arguments (directory name, start number, and end number) and creates the specified number of directories with dynamic names. For example, ./createDirectories.sh day 1 90 should create directories from "day1" to "day90".

Here's a bash script createDirectories.sh that creates the specified number of directories with a dynamic directory name:

#!/bin/bash

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

directory_name=$1
start_number=$2
end_number=$3

if ! [[ "$start_number" =~ ^[0-9]+$ ]] || ! [[ "$end_number" =~ ^[0-9]+$ ]]; then
  echo "Error: Start and end numbers must be integers."
  exit 1
fi

if [ $start_number -gt $end_number ]; then
  echo "Error: Start number must be less than or equal to the end number."
  exit 1
fi

for ((i=start_number; i<=end_number; i++))
do
  dir_name="${directory_name}${i}"
  mkdir "$dir_name"
done

echo "Directories created successfully."

Save the above script in a file called createDirectories.sh. Then, make it executable by running the following command in the terminal:

chmod +x createDirectories.sh

ou can execute the script by providing the directory name, start number, and end number as arguments. Here's an example usage:

./createDirectories.sh day 1 90

This will create 90 directories named day1, day2, day3, and so on, up to day90.

🔹Create a Script to backup all your work done till now.

#!/bin/bash
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
df -H | awk '{print $5 " " $1}' | while read output;
do
  # echo "disk details: $output"
  usage=$(echo $output | awk '{print $1}' | cut -d'%' -f1)
  filename=$(echo $output | awk '{print $2}')
  if [ $usage -gt 90 ]
  then
    echo "critical $filename $timestamp"
fi
done

This script is a bash script that checks disk usage and identifies if any disks are critically full (usage greater than 90%). Let's break down how it works:

  1. #!/bin/bash: This line indicates that the script should be interpreted and executed using the bash shell.

  2. df -H | awk '{print $5 " " $1}' | while read output;: The df -H command is used to display the disk usage statistics in a human-readable format. The output is then piped to awk to extract the fifth and first fields (disk usage percentage and disk mount point) from each line. The while read output loop reads each line of the output one by one and stores it in the variable output.

  3. usage=$(echo $output | awk '{print $1}' | cut -d'%' -f1): This line uses awk and cut to extract the numeric percentage value of disk usage from the variable output and stores it in the variable usage.

  4. filename=$(echo $output | awk '{print $2}'): This line uses awk to extract the disk mount point (filename) from the variable output and stores it in the variable filename.

  5. echo $usage: This line simply prints the disk usage percentage.

  6. if [ $usage -gt 90 ]: This is an if statement that checks if the value of usage is greater than 90%.

  7. then: If the condition in the if statement is true (i.e., disk usage is greater than 90%), the following block of code is executed.

  8. echo "critical $filename": This line prints a message indicating that the disk mounted at filename has a critical disk usage of more than 90%.

chmod +x backup.sh
./backup.sh

🔹What is Cron and Crontab how we can Automate the Backup Script?

🔹Cron

Cron is the primary system scheduler in Unix-like operating systems. It allows users to schedule and run tasks or jobs at specific intervals without the need for manual intervention. These tasks can be executed daily, weekly, monthly, or even at custom time intervals.

Cron is particularly useful for automating repetitive tasks, such as backups, system maintenance, and periodic scripts.

🔹 Crontab

Crontab is a command that allows users to manage their cron job entries. It provides a simple interface to submit, edit, and delete cron jobs. Each user on the system can have their own crontab file, where they can define the schedule and command for the tasks they want to automate.

Here's an example of how you can set up the backup script to run daily at midnight using crontab:

  1. Open the crontab for editing using the command: crontab -e

  2. Add the following line to the crontab file to schedule the backup script:

0 23 * * * /path/to/backupScript.sh >> /path/to/backup.txt

🔹User Management in linux

  1. Creating a User: To create a new user, you can use the useradd command followed by the desired username:
sudo useradd username
  1. Checking User and Groups: To check user information and group memberships, you can use the id command followed by the username:
id username

This will display output similar to:

uid=1001(john) gid=1001(john) groups=1001(john)

Here, "uid" represents the user's UID, "gid" is the primary group ID, and "groups" are the supplementary group IDs that the user belongs to.

  1. Deleting a User:To delete a user, you can use the userdel command followed by the username:
sudo userdel username
  1. Changing User Password:To change a user's password, you can use the passwd command followed by the username:
sudo passwd username

After executing the passwd command, you will be prompted to enter the new password for the user.

Remember to use sudo before the commands that require administrative privileges.