Skip to main content

Command Palette

Search for a command to run...

Day 5 - #90DaysOfDevOps

Basic Linux Shell-Scripting

Published
4 min read
Day 5 - #90DaysOfDevOps
H

Hemant is a Microsoft certified SharePoint Administrator and Power platform developer with 16+ years extensive experience in IT, Microsoft SharePoint. He has decade involved providing M365 and SharePoint for business problems across nearly all industries and commercial sectors. Also he involved in Management/Leadership of Projects in Energy, Healthcare, Content Management, Mining domain, gaining extensive expertise in Project Lifecycle Management, Agile Development, DevOps Testing Cycle as well as Team Management

• Expertise in all aspects of the SharePoint Administration, installation, configuration, development, architecture, deployment and Migrations

• Experienced in developing collaboration and power platforms, portals, enterprise search, enterprise content management, business processes of SharePoint in large enterprise environments.

• Providing day-to-day administration and support on monitoring, usage and growth analysis and patching on SharePoint intranet and Internet.

• Adopting new technology and implementing them, in-depth functional knowledge with appreciation for technical skills.

• Have been involved in various phases of SDLC including analysis, design, coding, testing and implementation mainly Agile development

• Collaborated in governance development and processes, enhancing system efficiency and adherence to best practice. Improving SharePoint capabilities by evaluating and integrating third-party solutions

Programming and Scripting: Proficient in PnP PowerShell scripting for automation and customization.

Learn more about these cloud projects by visiting my portfolio at https://medium.com/@risbud

Technologies interested Cloud | MultiCloud | AWS | DevOps | Microsoft Azure | Google Cloud | Terraform | Ansible

💻What is Kernel?

The Linux® kernel is the main component of a Linux operating system (OS) and is the core interface between a computer’s hardware and its processes.

The kernel has 4 jobs:

  1. Memory management: Keep track of how much memory is used to store what, and where

  2. Process management: Determine which processes can use the central processing unit (CPU), when, and for how long

  3. Device drivers: Act as mediator/interpreter between the hardware and processes

  4. System calls and security: Receive requests for service from the processes

💻📜⚙️What is a Shell?

It refers to a special user program or an environment that provides a user with an interface for using the services of the operating system. A shell executes various programs based on the input that a user provides.

It is a core of a shell script a text file containing a set of series of commands that the shell interpreter can execute. When you run a shell script, the commands in the script are executed line by line.

Let us talk about the differences present between Shell and Kernel.

Parameters

Shell

Kernel

Basics

A shell is basically an interface present between the kernel and the user.

A kernel is the very core of a typical OS.

Meaning

A shell is a CLI (command-line interpreter).

A kernel is a type of low-level program that has it's interfacing with the hardware on top of which all the applications run (disks, RAM, CPU, etc.).

Uses and Purpose

A shell allows all of its users to establish communication with the kernel.

A kernel functions to control all the tasks that come with a system.

Types

Korn Shell, C Shell, Bourne Shell, etc., are types of shells.

Hybrid kernel, Micro-kernel, Monolithic kernel, etc., are types of kernels.

Functions

We can use shell commands such as mkdir, ls, and many more to request the completion of the specific operation to the operating system (OS).

A kernel carries out the commands on a group of various files by specifying the pattern that can match.

Management

A shell performs memory management.

A kernel performs process management.

Layer of OS

The shell forms the outer layer of the operating system.

The kernel forms the inner layer of the operating system.

Machine-Understandability

A shell interacts with all of its users and then interprets into a language that is understandable by the machine.

A kernel interacts with the hardware directly because it accepts the machine-understandable language from the available shell.

🤔 What is #!/bin/bash? Can we ✍️ #!/bin/sh as well?

Both #!/bin/bash and #!/bin/sh is shebang lines that tell the operating system which interpreter to use to execute the shell script.

  • #!/bin/bash - Use the Bash shell interpreter to run the script. Bash is the most common and featureestic shell available on Linux.

  • #!/bin/sh - Use the Bourne shell (/bin/sh) interpreter. This is the most basic shell available on the Linux system.

🐚📜 Write a Shell Script which prints

#!/bin/bash
echo "This is some text"

Take input from arguments 🅰️🅱️

  #!/bin/bash

  echo "Your name is $1" 
  echo "Your surname is $2"

Display the variables 🖥️📤

echo "Your name is $variable"

🖥️Create directories based on a given range and precedence. Save the bash file as createdirs.sh It takes 3 arguments like - createdirs.sh movies 15 30📤

#!/bin/bash
#check if the script has three arguments
if [ "$#" -ne 3 ]; then
   echo "Usage: $0 <directory name> <start no> <end no>"
   exit 1
fi
# assign the provided args to variables
directory_name="$1"
start_num="$2"
end_num="$3"
# validate that start and end numbers are integers
if ! [[ "$start_num" =~ ^[0-9]+$ ]] || ! [[ "$end_num" =~ ^[0-9]+$ ]]; then
   echo "start and end numbers must be integers"
   exit 1
fi
# create dirs with dynamic names
for ((i = start_num; i <= end_num; i++));do
   dir_name="${directory_name}_${i}"
   if [ -d "$dir_name" ];then
      echo "Directory name $dir_name already exists. Skipping..."
   else
      mkdir "$dir_name"
      echo "Directory created: $dir_name"
   fi
done

O/P:

🖥️ Another shell program that backs up your work, save as backupwork.sh .

#!/bin/bash
#source directory
source_dir="/home/mobaxterm/Desktop/temp"
backup_dir="/home/mobaxterm/Desktop/twoTierApp/linux"
#createe timestamp for the backup file
timestamp=$(date +'%Y%m%d')
backup_folder="$backup_dir/backup_$timestamp"
#check if source directory exists
if [ ! -d "$source_dir" ]; then
    echo "Source directory does not exists"
    exit 1
fi
#create backup directory
if [ ! -d "$backup_folder" ]; then
    mkdir -p "$backup_folder"
fi
#perform backup
cp -r "$source_dir" "$backup_folder"
#check if backup was successful
if [ $? -eq 0 ]; then
    echo "Backup completed successfully".
else
    echo "Backup failed"
fi

You need to give execute permission 700 or u+x

O/P:

Thank you for reading this article/Blog!