Day-5 : Advance Shell Scripting Used in DevOps with User management

Day-5 : Advance Shell Scripting Used in DevOps with User management

Table of contents

No heading

No headings in the article.

  1. Write a script that when the script is executed with three given arguments (one is the directory name and the second is the start number of directories and the third is the end number of directories )

Use vi or vim to create a .sh file then, write the script and save it by pressing the ESC button and then entering:wq!

Don't forget to change file permission otherwise, you will face a Permission denied error.

To check file permission type ls -l and to change permission type chmod 700 and create directories.sh

This script will take parameters from the user then it will create directories. during running, a file faced a Syntax error: Bad for loop variable.

I faced an error because I was trying to run a file by typing sh createDirectories.sh so then I tried to run the script like ./createDirectories.sh then it runs perfectly.

The below code will first accept the directory name which the user wants to create and then will access the start number and end number from the user.

#!/bin/bash

echo "Enter Directory Name:"
read dir_nm
echo "Enter Start Number:"
read start_nm
echo "Enter End Number:"
read end_nm


for (( i =$start_nm; i <=$end_nm; i++))
do
         mkdir "$dir_nm$i"
         echo "$dir_nm$i"
done

Write a Script to back up all your work done till now

Now let's look at below source code below step by step.

  1. It will accept a filename or directory name from the user.

  2. It will accept the path from the user where the user wants to take a backup of files.

  3. I have used the find command to search files or a directory. It will search the file or a directory that the user has entered.

  4. Now I have used the tar command to zip files.

    c – Create
    z – z is used for zip compression
    v – verbal, it will show information about the archive while archiving.
    f – using an archive file.

#!/bin/bash

#take a parameter from user which files or directory wants to take backup.
echo "Enter filename to take backup of files"
read filename
#Allow user to enter path where where wants backup of files.
echo "Enter path to take backup of files"
read path

#below command will search files
find / -name "filename"

tar czvf $path$filename.tar.gz $filename

echo "Backup Finished"

Write a script to automate the backup using cron and crontab.

Let's understand why we are using corn and crontab. It is used to take a backup of files in an automated way. we can plan our backup as per our time. it means we can schedule our backup by mentioning time, Hour, Day Date, Month, and Week.

#!/bin/bash

src_path="/home/ubuntu/devops/"
dest_path="/home/ubuntu/backup"


tar czvf $dest_path.tar.gz $src_path


echo "Backup Finished"

crontab -l will list the active crontab list.

crontab -e is used to edit the crontab file.

crontab - r is used to remove all cron jobs.

enter the crontab -e command then enter the below-mentioned code.

What are these * * * * * 5 stars in the crontab -e file entered?

* * * * * ==> 5 stars means the script should run daily every minute each hour.

What is User Management in Linux?

useradd command is used to add a new user.

passwd command is used to create passwords for user.

usermod command is used to change the user id.

userdel command is used to delete a user.

Create 2 users and just display their username

sudo useradd dev1
sudo useradd dev2
cat /etc/passwd | grep "dev"

Thank You for reading this blog

Parimal Pradhan

You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

#devops #90daysofdevops

Did you find this article valuable?

Support Parimal Pradhan by becoming a sponsor. Any amount is appreciated!