Advanced Linux Shell Scripting for DevOps
About Shell Scripting, Crontab & User Management
Table of contents
No headings in the article.
Day 5 of #90DaysOfDevOps with the #TrainWithShubham Community. The challenge is for the DevOps Community to get stronger in #DevOps. It is a great initiative by Shubham Londhe. Documenting my learning and also sharing it with the community is helping me to get more clear with the concepts.
- Write a bash script createDirectoriess1.sh that when the script is executed with three given arguments (one is the directory name and second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.
- Use shell scripting and take the input as an argument from the user.
Scenario 1: Creates 90 directories as day1 day2 day3 .... day90
Scenario 2: Creates 50 directories as Movie20 Movie21 Movie23 ...Movie50
2. Create a Script to back up all your work done till now.
- Write a shell script, whenever this script runs it back up the source file to the backup directory as a tar file and the name would be the time stamp when it runs.
Output:
*tar xvf
use to extract the tar file.
3. Read About Cron and Crontab, to automate the backup Script
The cron is a software utility, offered by a Linux-like operating system that automates the scheduled task at a predetermined time. It is a daemon process, which runs as a background process and performs the specified operations at the predefined time when a certain event or condition is triggered without the intervention of a user.
The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. Crontab stands for “cron table, ” because it uses the job scheduler cron to execute tasks. The schedule is called the crontab.
To view the Crontab entries
crontab -l
View Root Crontab entries: Login as root user (
su – root
) and docrontab -l
To view the crontab entries of other Linux users: Log in to root and use
crontab -u {username} -l
.To edit Crontab Entries
rontab -e
Crontab Fields and Allowed Ranges (Linux Crontab Syntax)
Field Description Allowed Value
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.
Linux Crontab Format
MIN HOUR DOM MON DOW CMD
Output:
4. Read about User Management
A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system.
The ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users hence the ids for local user begins from 1000 onwards.
To list out all the users in Linux
awk -F':' '{ print $1}' /etc/passwd
Using id command, you can get the ID of any username.
id username
The command to add a user.
sudo useradd username
Using passwd command to assign a password to a user
passwd username
5. Create 2 users and just display their Usernames.
Output:
Thank you.