DMCA.com Protection Status Trending Topics About Devops: Advanced Linux Shell Scripting for DevOps Engineers

Thursday, 28 September 2023

Advanced Linux Shell Scripting for DevOps Engineers

 

TABLE OF CONTENTS

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

2.create a Script to backup all your work done till now

3.Read About Cron and Crontab, to automate the backup Script

4.Read about User Management

5.Create 2 users and just display their Usernames

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

Execution,

./directories.sh day 1 90

Here,

day, — First argument

1, — Second argument

2, — Third Argument

2.create a Script to backup all your work done till now

##########################################################################
#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

Above, script will take backups.

3.Read About Cron and Crontab, to automate the backup Script

crontab -e

Enter 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 month

fourth *, — represents month

fith *, — day of week

4.Read about User Management

User management is a very important part of DevOps engineer’s journey.

User management includes some basic tasks, like

  • Creating Users
  • Deleting Users
  • Password reset
  • Adding user in a group

5.Create 2 users and just display their Usernames

##########################################################################
#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

No comments: