Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management
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.
Let's create a script in vim editor with the name createDirectories.sh
#!/bin/bash dir_name=$1 start_range=$2 end_range=$3 echo "******************Creating Directoris******************" for (( i=start_range; i<=end_range; i++ )) do sub_dir=${dir_name}${i} mkdir "${sub_dir}" echo "creating diretory --> $sub_dir" done echo "************Directoris Created Successfully************"
Here, $1, $2 and $3 are the arguments that are to be passed while executing the script in our terminal.
Create a Script to backup all your work done till now.
#Diretory to be backed up
source_dir=/home/ubuntu/day5_q2
#Backup destination directory
backup_dir=/home/ubuntu/backup_day5_q2
#create backup filename
backup_file=$(date +'%d-%m-%y_%H%M%S').tar.gz
echo "******* Backup Started*****"
#create backup using tar
tar -cvf "${backup_dir}/$backup_file" "$source_dir"
echo "******Backup Completed*****"
Create 2 users and just display their Usernames
To create user:
sudo useradd -m Devops_student1 sudo useradd -m Devops_student2
To set a password:
ubuntu@ip-172-31-89-192:~$ sudo passwd Devops_student1 New password: Retype new password: passwd: password updated successfully ubuntu@ip-172-31-89-192:~$ ubuntu@ip-172-31-89-192:~$ sudo passwd Devops_student2 New password: Retype new password: passwd: password updated successfully ubuntu@ip-172-31-89-192:~$
To view users:
To create a group:
sudo groupadd deevops_community
To add users to a group:
sudo usermod -aG deevops_community Devops_student1 sudo usermod -aG deevops_community Devops_student2
To view users in a group:
Conclusion
In this blog, we explored a range of powerful commands that empower DevOps practitioners to streamline processes, improve productivity, and enhance system performance.
Thank you for reading! Keep Supporting, learning and growing with each other.