Skip to main content

Command Palette

Search for a command to run...

Advanced Linux Shell Scripting for DevOps Engineers with User management (Day-5 Task)

Published
3 min read
Advanced Linux Shell Scripting for DevOps Engineers with User management (Day-5 Task)

#trainwithshubham # 90DaysOf DevopsChallenge #devops

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

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

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:

First, make a directory called backup and another directory which is already created previously shell_scripts( previous work done within this folder). We want to take backup daily so printed a current timestamp.

Output :

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 :

More from this blog

Devops

49 posts