Automate shell script to finish task 1 in linux challenge

After I finished the Shell Scripts for Beginners course, i wanted to practice what i learned, that’s why i made a script to improve skill .
i am a beginner and i hope for any correction to the script to make it shorter and any other tips to improve .
i want to say thanks kodekloud team
thanks Mumshad Mannambeth



Just copy the code.

#!/bin/bash

#######################################
#Automate script to finish task 1 in linux challenge
#Author: Motasem Aljaafreh 
#Email: [email protected]
#Date created : 11/9/2022
#######################################

if [ $UID -ne 0 ]
then
        echo "Please usage with sudo or as root"
        exit 1
fi
#######################################
# i got this function color from shell scripts for beginners course.
# Author : Mumshad Mannambeth
#######################################

function print_color(){
  NC='\033[0m' # No Color

  case $1 in
    "green") COLOR='\033[1;32m' ;;
    "red") COLOR='\033[0;31m' ;;
    "yallow") COLOR='\033[5;33m';;
    "*") COLOR='\033[0m' ;;
  esac

  echo -e "${COLOR} $2 ${NC}"
}


#Install the correct packages that will allow the use of "lvm" on the centos machine.
sudo yum install lvm2 -y  &>/dev/null
if [[ $? -eq 0 ]]
then
       print_color "green" ".............. LVM Package installed Successfully ................."
else
       print_color "red" ".............. LVM Packageis Not installed ................."
       exit 1
fi

#Create a Physical Volume for "/dev/vdb" "/dev/vdc".
sudo pvcreate /dev/vdb /dev/vdc &>/dev/null
if [[ "$?" -eq 0 ]]
then
        print_color "green" ".............. Physical Volume Created Successfully ................."
else
        print_color "red" ".............. Physical Volume is Not Created ................."
        exit 1
fi

#Create a volume group called "dba_storage" using the physical volumes "/dev/vdb" and "/dev/vdc".
sudo vgcreate dba_storage /dev/vdb /dev/vdc &>/dev/null
if [[ "$?" -eq 0 ]]
then
        print_color "green" ".............. Volume Group Created Successfully................. "
else
        print_color "red" ".............. Volume Group is Not Created ................."
        exit 1
fi

#Create an "lvm" called "volume_1" from the volume group called "dba_storage".
sudo lvcreate -n volume_1 -l 100%FREE dba_storage &>/dev/null
if [[ "$?" -eq 0 ]]
then
        print_color "green" ".............. Logical Volume Created Successfully ................."
else
        print_color "red" ".............. Logical Volume is Not Created ................."
        exit 1
fi

#Format the lvm volume "volume_1" as an "XFS" filesystem.
sudo mkfs.xfs /dev/dba_storage/volume_1 &>/dev/null
if [[ $? -eq 0 ]]
then
        print_color "green" ".............. XFS Filesystem Formatted Successfully ................."
else
        print_color "red" ".............. XFS Filesystem is Not Formatted ................."
        exit 1
fi

#Create directory name "dba_storage" under /mnt/ .
sudo mkdir  /mnt/dba_storage &>/dev/null
if [[ $? -eq 0 ]]
then
        print_color "green" ".............. Directory dba_storage Created Successfully ................."
else
        print_color "red" ".............. Directory is Not Created ................."
        exit 1
fi

#Make sure that this mount point is persistent across reboots with the correct default options.
echo "/dev/mapper/dba_storage-volume_1 /mnt/dba_storage xfs defaults 0 0" >> /etc/fstab

#Create a group called "dba_users" and add the user called 'bob' to this group.
sudo groupadd dba_users
if [[ $? -eq 0 ]]
then
        print_color "green" ".............. Group dba_users Created Successfully ................."
else
        print_color "red" ".............. Group is Not Created ................."
        exit 1
fi

#Create a group called "dba_users" and add the user called 'bob' to this group.
sudo usermod -G dba_users bob
if [[ $? -eq 0 ]]
then
        print_color "green" ".............. bob user Has Been Added to dba_users Group Successfully ................."
else
        echo "red" ".............. bob user Has Not Been Added to dba_users Group ................."
        exit 1
fi


#Mount the filesystem at the path "/mnt/dba_storage.
sudo mount -a
if [[ $? -eq 0 ]]
then
        print_color "green" ".............. Partition Volume_1 Mounted Successfully ................."
else
        print_color "red" ".............. Partition is Not Mounted ................."
        exit 1
fi

#Ensure that the mount point "/mnt/dba_storage" has the group ownership set to the "dba_users" group
sudo chgrp dba_users /mnt/dba_storage
if [[ $? -eq 0 ]]
then
        print_color "green" ".............. Ownership of Directory /mnt/dba_storage Changed Successfully ................."
else
        print_color "red" " .............. Ownership of Directory /mnt/dba_storage is Not Changed ................."
        exit 1
fi

#Ensure that the mount point "/mnt/dba_storage" has "read/write" and execute permissions for the owner and group. 
sudo chmod 770 /mnt/dba_storage
if [[ $? -eq 0 ]]
then
        print_color "green" ".............. Permission /mnt/dba_storage Changed ................."
else
        print_color "red" ".............. Permission /mnt/dba_storage is Not Changed ................."
        exit 1
fi

print_color "yallow" "................. Your Task is Completed - Congratulations .................. "

exit 0

Awesome, @Motasem-Aljaafreh

1 Like

Can you do with uuid. Iam struck with uuid “error saying is a directory”

Hi @yegnasivasai ,
Don’t worry about that. We do have a solution repo on our GitHub page.

Please check it out.

Regards,