Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management


Tasks
Q1. 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
#!/bin/bash
dir_name=$1
fir_no=$2
end_no=$3
for((i=fir_no;i<=end_no;i++))
do
mkdir -p /home/akshatha/shell_script/new/$1$i
done
output: ./
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
Q2.Create a Script to backup all your work done till now.
#!/bin/bash
backup_dir="/home/akshatha/backup" #Define the backup directory
mkdir -p $backup_dir #Create the backup directory if does not exists
timestamp=$(date +'%Y%m%d_%H%M%S')
backup_filename="backup_${timestamp}.tar.gz"
tar czf "${backup_dir}/${backup_filename}" /home/akshatha/shell_script/
echo "Backup created: ${backup_dir}/${backup_filename}"
o/p:
Q3:Let's Create Crontab file
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.
Command to create crontab file is
crontab -e
Add below line in your crontab file (Note: Check your date in your command prompt and give your own timestamp)
30 14 \ * * s*h /home/akshatha/shell_script/scripts_shell/backup_script.sh
Above Output backup is created with name as backup_20231226_143001.tar.az as given in script file(backup_filename="backup_${timestamp}.tar.gz)
here {timestamp} is in the format of {year%month%date_%hour%min%sec}
Q4: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. After installation of 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 and hence the ids for local user begins from 1000 onwards.
**Q5:**Create 2 users and just display their Usernames
sudo useradd <username>
Subscribe to my newsletter
Read articles from Akshatha Chendel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
