- DevOps Lifecycle -Newspaper WordPress Theme
90DaysOfDevOpsDay-5 Task - Advanced Linux Shell Scripting for DevOps Engineers with User...

Day-5 Task – Advanced Linux Shell Scripting for DevOps Engineers with User Management

Challenge:

If you noticed that there are total 90 sub directories in the directory ‘2023’ of this repository. What did you think, how did I create 90 directories. Manually one by one or using a script, or a command ?

All 90 directories within seconds using a simple command.

mkdir day{1..90}

Tasks

  1. You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments –

So Write a bash script createDirectories.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.

Example 1: When the script is executed as

./createDirectories.sh day 1 90

then it creates 90 directories as day1 day2 day3 .... day90

Example 2: When the script is executed as

./createDirectories.sh Movie 20 50 then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50

As we know that if we want to pass any argument at the time of bash script execution we used to denote it as like $1(first arg),$2(2nd arg) etc…So here we will do the same thing in a bash script like :

name of the file as createDirectories.sh

start_day as $2 and end_day as $3 and day as $1

Execute as ./createDirectories.sh day 1 90

Output

#!/bin/bash

if [ $# != 3 ]
then
	echo "Can't proceed \n Please give three arguments to continue"
	exit 1

fi

echo "Creating directory as per your requirement"
start_day=$2
end_day=$3

for (( i=start_day; i<=end_day; i++ ))
do
	mkdir $1$i
done	

echo "Here are your directories:"
ls

2. Create a Script to back up all your work done till now.

Backups are an important part of DevOps Engineers’ day to Day activities. Usually, when we create any backup of a file we used to create a zip file by using the tar command: tar -czvf file.tar.gz directory_name

Here is a simple shell script to take a backup of recent work:

#!/bin/bash

src_directory=/var/www/my-blog

BACKUP_FILENAME="backup_$(date +%Y-%m-%d).tar.gz"
TARGET_DIRECTORY="/home/ubuntu/backup/"

#Create backup archive
tar -czvf $TARGET_DIRECTORY/$BACKUP_FILENAME $src_directory

if [ $? -eq 0 ]; then
	echo "Backup successful!"
else
	echo "Backup failed."
fi

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

Cron is a system process that will automatically perform tasks as per the specific schedule. It is a set of commands that are used for running regular scheduling tasks. Crontab stands for “cron table”. It allows using a job scheduler, which is known as cron to execute tasks.

Crontab is also the name of the program, which is used to edit that schedule. It is driven by a crontab file, a config file that indicates shell commands to run periodically for the specific schedule.

Reasons for using Cron jobs:

  • Helps OS to take a scheduled backup of log files or database.
  • Delete old log files
  • Archive and purge database tables
  • Send out any notification email such as Newsletters, Password expiration email
  • Regular clean-up of cached data
  • Crontab is an ideal option to automate Unix jobs.
  • It is used to automate system maintenance

Crontab syntax:

MIN HOUR DOM MON DOW CMD

Ranges :

4. Read about User Management

User management includes everything from creating a user to deleting a user on your system.

root

The root user is the superuser and has all the powers for creating a user, deleting a user and can even login in with the other user’s account. The root user always has userid 0.

useradd

1. With useradd command, you can add a user.

Syntax: sudo adduser username [you should have the super user power to add a user]

Then Enter the password for the new account and confirm

You can check the newly added user from the file /etc/passwd

Command: cat /etc/passwd

2. For disabling an account using Terminal, remove the password set on the account.

sudo passwd -l ‘username’

3. To delete an account, use the command –

sudo userdel -r ‘username

5. Create 2 users and just display their Usernames

Added two users by using the useradd command :

$ sudo useradd Sam

$ sudo useradd Vinod

To display their name use cat /etc/passwd and redirected the output to the tail command to see only the last two lines from the file :

To display their name use cat /etc/passwd and redirected the output to the tail command to see only the last two lines from the file :

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

SUPPORT NONPROFIT JOURNALISM

EXPERT ANALYSIS OF AND EMERGING TRENDS IN CHILD WELFARE AND JUVENILE JUSTICE

TOPICAL VIDEO WEBINARS

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Exclusive content

- Advertisement -Newspaper WordPress Theme

Latest article

More article