Advanced Linux Shell Scripting for DevOps Engineers with User Management

Imran ShaikhImran Shaikh
3 min read
  1. Create Directories Using Shell Script:

    • Write a bash script createDirectories.sh that, 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.sh day 1 90, it creates 90 directories as day1 day2 day3 ... day90.

    • Example 2: When executed as ./createDirectories.sh Movie 20 50, it creates 31 directories as Movie20 Movie21 Movie22 ... Movie50.

    #!/bin/bash

    #Check number of arguments

    if [ "$#" -ne 3 ]
    then
            echo "Enter valid arguments $0"
    exit 1
    fi

    # Arguments stored in variables which is entered by user

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

    #forloop to create directories

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

    echo "Directories created from ${dir_name}${start_num} to ${dir_name}${end_num}"

$# — Number of Arguments

$1 — First Arguments

$2 — Second Arguments

$3 — Third Arguments

Execution of Example 1:

Execution demo of Example 2:

  1. Create a Script to Backup All Your Work:
  • Backups are an important part of a DevOps Engineer's day-to-day activities. (it can feel a bit difficult but keep trying, nothing is impossible).
#!/bin/bash

#Specify Path of Source Directory and store in Variable

backup="/home/ubuntu/"

#Specify Path of Destination Directory and store in Variable

dest="/imp/backup/"

#Create Directory as Destination Path

mkdir -p "$dest"

#Store date along with timestamp

day=$(date +%a-%d-%b-%y-%H%M)

#Store hostname without domain name

hostname=$(hostname -s)

#Assign backup file name

archive="$hostname-$day.tar"

#Create tar file

tar -cvf "$dest/$archive" -C "$backup" .

#Print message
echo "Backup Finished"
  • %a: represents the weekday name (e.g., Mon, Tue)

  • %d: represents the day of the month (01 to 31)

  • %b: represents the month name (e.g., Jan, Feb)

  • %y: represents the last two digits of the year, such as 24 for the year 2024 (00 to 99)

  • %H: represents the hour in 24-hour format (00 to 23).

  • %M: represents the minute (00 to 59).

Execution of Script

—> Backup file will store in /imp/backup/

  1. 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.

  • Cron : Cron is daemon that executes scheduled commands. It also reads /etc/crontab

  • Crontab : Crontab is the program used to install, deinstall or list the table used to drive the cron

  • Check Cron table list by using following command:

#List the crontab

crontab -l
  • Edit crontab by using following command:
#Edit crontab

crontab -e
  • Delete crontab by using following command:
#To Delete crontab

crontab -r
  • Cron Format :
Minute   Hour   Day of Month    Month    Day of Week  Command/Script
(0-59)  (0-23)     (1-31)     (1-12 or     (0-6 or     
                               Jan-Dec)     Sun-Sat)
  1. 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.

  • Creating users:

#!/bin/bash

#Create user

useradd imran
useradd shaikh

#Display created users

sudo cat /etc/shadow | grep -i imran
sudo cat /etc/shadow | grep -i shaikh
  • Output:

  • Happy Learning:)

0
Subscribe to my newsletter

Read articles from Imran Shaikh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Imran Shaikh
Imran Shaikh