Advanced Linux Shell Scripting for DevOps Engineers with User Management

Advanced Linux Shell Scripting for DevOps Engineers with User Management

·

2 min read

Advanced Linux Shell Scripting for DevOps Engineers can cover a wide range of topics, but one area that may be particularly relevant to DevOps is user management. In this context, shell scripts can be used to automate the creation, modification, and deletion of user accounts, as well as other related tasks such as managing groups and permissions.

Write a bash script create directories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.

#!/bin/bash
#############################################################
#Author: Tejal Girme
#Date: 05/01/2024
#Purpose: This script will create 90 directories at once
#############################################################

#Intializing variables
Name_of_dir=$1
start_no=$2
end_no=$3

#Command to create directories
eval mkdir $Name_of_dir{$start_no..$end_no}

Here , "eval" command takes arguments as input to the command and stores it as one single command.

Execute

./directories.sh day 1 90

Here

day --> First argument
1 --> Second argument
2 --> Third argument


Create a Script to backup all your work done till now

##########################################################################
#Author: Tejal Girme
#Date: 05/01/2024
#Purpose: This script will take backup
##########################################################################

#Creating Variables 
src=/home/ubuntu/day5
dest=/home/ubuntu/backups
time=$(date +"%Y-%m-%d-%H-%M-%S")
backupfile=$dest/$time.tgz

#Taking Backup
echo "Taking backup on $time"
tar zcvf $backupfile --absolute-names $src

if [ ${?} -eq 0 ]
then
        echo "Backup Complete"
else
        exit 1
fi

👆this script will take backups


Read About Cron and Crontab, to automate the backup Script

crontab -e

Add the below code in the last line of crontab

* * * * * sh /home/ubuntu/day5/backup.sh

first * --> represents minutes
second * --> represents hours
third * --> represents day of the month
fourth * --> represents a month
fifth * --> day of the week


Read about User Management

User management is a very important part of DevOps Engineer's Journey.

User management includes some basic tasks like

  • Creating User

  • Deleting User

  • Password reset

Adding user in a group


Create two users and just display their Usernames

##########################################################################
#Author: Tejal Girme
#Date: 05/01/2024
#Purpose: This script will take two argument as username and create user
##########################################################################

#Creating variables
user1=$1
user2=$2

#Creating Users
echo "Adding Users $user1 and $user2"
useradd -p test -m $user1
useradd -p test -m $user2

if [ ${?} -eq 0 ]
then
        echo "Users added successfully"
else
        exit 1
fi

Happy Learning!!

Thanks For Reading 🙂