Day 5 - Advanced Linux Shell Scripting
DevOps Engineers with User management
If you noticed that there are a total of 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
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 :-
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.
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
Notes: You may need to use loops or commands (or both), based on your preference. Check out this reference: https://www.geeksforgeeks.org/bash-scripting-for-loop/
Ans :-
2. Create a Script to backup all your work done till now.
Backups are an important part of DevOps Engineer's day to Day activities
The video in References will help you to understand How a DevOps Engineer takes backups (it can feel a bit difficult but keep trying, Nothing is impossible.)
Watch [this video](https://youtu.be/aolKiws4Joc)
Ans :-
Now, run this script on crontab, set crontab with command crontab -e
the crontab is running on the background
go to dir new_tech and check file when cronjob is done
3 . Read About Cron and Crontab, to automate the backup Script
Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit or delete entries to cron. A crontab file is a user file that holds the scheduling information.
Watch This video as a Reference to Task 2 and 3 [[youtu.be/aolKiws4Joc]](https://youtu.be/aol..
Cron :- Cron is a Linux job scheduler that is used to set up tasks to run periodically at a fixed date or interval. Cron jobs are specific commands or shell scripts that users define in the crontab files. These files are then monitored by the Cron daemon and jobs are executed on a pre-set schedule.
Crontab :- The crontab
file is a simple text file that instructs the cron
daemon to perform a task at a certain time or interval. Any user may schedule cron
tasks or jobs on a system. The task runs under the user account from which it was created. In other words, if you create a cron
task, it runs with your user account's permissions. The same is true for any user on the system, including the root user.
Location of crontab :- cat /etc/crontab
4 . Read about User Management
User management is the process of managing different user accounts and their respective permissions in an operating system. In Linux, we can create different user accounts, sort them into groups, change their set of permissions or delete them.
User management includes everything from creating a user to deleting a user on your system.
To create a user account
#useradd Sajid
To check user account properties
#sudo cat /etc/passwd
To create a user account password
#passwd Sajid
For switching user account
su Sajid
For logout from the user account
exit
To delete a user account
userdel Sajid
Group Management
A group is a collection of users' accounts Users can be listed in different groups. Group allows us to set permission on the group level.
To add a group account
#groupadd dev_sajid
To check group account property
#sudo cat /etc/group
To delete the group account
groupdel dev_sajid
To add a single member to a group
#gpasswd -a Sajid dev_sajid
To add multiple members to a group
#gpasswd -M sajid,rajiv,sachin,sunny dev_sajid
5 . Create 2 users and just display their Usernames
To see all user :- sudo cat /etc/passwd
* END **