Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Basic and Advance Linux Scripting
6 students

Basic and Advance Linux Scripting

With this course you will learn Shell Scripting from scratch
Created byAaftab Hamdani
Last updated 1/2024
Hindi

What you'll learn

  • Basic and Advance Linux Scripting
  • Linux Automation
  • Various Loops
  • Conditions
  • Functions

Course content

3 sections14 lectures9h 38m total length
  • Scripting Session 150:07

    Shell scripting session 1

    What is shell

    - Shell is an interpreter between kernal and user


    Types of shell

    1. Sh shell

    2. BASH shell

    3. Cshell

    4. kshell



    For out identitiy will use .sh extension


    #!/bin/bash -- Shebang (It is always the first line of script)

    chmod a+x abc.sh   --- adding execute permission on abc.sh

    There are 2 ways to run bash script

    1- ./abc.sh  (x permission required)

    2- bash abc.sh


    Assigning variable


    var_name=value

    name=Aaftab


    echo "My name is $name"

    - here $ name is replace with Aaftab


    #!/bin/bash

    IP=$(ip a s | tail -4 | head -1 | tr -s " " | cut -d " " -f3 | cut -d "/" -f1)

    echo "My IP address is -----> $IP"


    note - if we want to assign the value of executed command in variable the  always used $()


    Bash script


    #!/bin/bash

    IP=$(ip a s | tail -4 | head -1 | tr -s " " | cut -d " " -f3 | cut -d "/" -f1)

    MEMORY=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f2)

    FMEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f4)

    MAC=$(ip a s | head -8 | tail -1 | tr -s " " | cut -d " " -f3)

    DISK=$(lsblk | head -2| tail -1 | tr -s " " | cut -d " " -f4)

    echo "My IP address is -----> $IP"

    echo "My IP Total memory is -----> $MEMORY"

    echo "My Free memory is -----> $FMEM"

    echo "My MAC is -----> $MAC"

    echo "My Disk size  is -----> $DISK"


    output -->


    My IP address is -----> 172.31.47.231

    My IP Total memory is -----> 761Mi

    My Free memory is -----> 319Mi

    My MAC is -----> 02:5e:b5:ba:41:26

    My Disk size  is -----> 10G

  • Scripting Session 239:30

    Scripting Session 2


    read -- when we want to take input from user then we use read

    ex-

    #!/bin/bash

    read -p "Enter your name -->" NAME

    echo "My name is $NAME"



    expr -- use to perform addition, substraction , multiplication adn division


    #!/bin/bash

    read -p "Enter first number -->" N1

    read -p "Enter second number -->" N2

    SUM=$(expr $N1 + $N2)

    echo "Addition of two number is $SUM"

    command line argument

    It is use to pass the argument at run time


    ex-

    #!/bin/bash

    SUM=$(expr $1 + $2)

    echo "Addition of two number is $SUM"


    $1 - first argument

    $2 - second

    $3- third

    etc....


    #!/bin/bash

    MEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f2 | cut -d "M" -f1)

    UMEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f3 | cut -d "M" -f1)

    UTI=$(expr 100 \* $UMEM / $MEM)

    echo "Memory utilization is $UTI%"


    output -- Memory utlization is 33%


    Task - 

    Free memory %

    Addition,multiplication, deivision, substraction

    Buffer cache in %

  • Scripting Session 337:36

    Scripting Session 3


    Condition - It is used to  execute as per the situation


    if [];then

    Syntax


    if [ condition ];then

    statement1

    statement2

    else

    Statement 3

    fi


    Syntx - if with elif

    if [ condition ];then

    statement1

    statement2

    elif [ condition ];then

    statement3

    else

    Statement 4

    fi



    Syntx with multiple conditions using operator


    if [ condition1 ] || [ condition2 ];then

    statement1

    else

    statement2

    fi


    operators

    ||  (or) -- it is used when either first or second condition should be match

    && (and) -- it is used when both condition should be match


    #!/bin/bash

    read -p "Please enter valid name-->" NAME

    if [ "$NAME" == "aaftab" ];then

    echo "Welcome to Pune"

    else

    echo "Invalid user"

    fi


    #!/bin/bash

    read -p "Please enter valid name-->" NAME

    if [ "$NAME" == "aaftab" ];then

    echo "Welcome to Pune"

    elif [ "$NAME" == "moin" ];then

    echo "welcome to Pune"

    else

    echo "Invalid user"

    fi


    #!/bin/bash

    read -p "Please enter valid name-->" NAME

    if [ "$NAME" == "aaftab" ] || [ "$NAME" == "moin" ];then

    echo "Welcome to Pune"

    else

    echo "Invalid user"

    fi


    Write script to verify if input value is more than 80 then print  first class between 60 to 80 second class and less than 60 third class


    #!/bin/bash

    read -p "Please enter Percentage-->" PER

    if [ $PER -gt 80 ];then

    echo "First class"

    elif [ $PER -gt 60 ] && [ $PER -lt 80 ];then

    echo "Second class"

    else

    echo "Third class"

    fi


    Write script to verify if input value is more than 80 then print  memroy alert between 60 to 80 memory warning and less than 60 normal memory utlization



    #!/bin/bash

    MEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f2 | cut -d "M" -f1)

    UMEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f3 | cut -d "M" -f1)

    UTI=$(expr 100 \* $UMEM / $MEM)

    if [ $UTI -gt 80 ];then

    echo "Memory Alert and utilization is $UTI%"

    elif [ $UTI -gt 60 ] && [ $UTI -lt 80 ];then

    echo "Memory warning and utilization is $UTI%"

    else

    echo "Memory is normal and utlization is $UTI%"

    fi


    lt -- less than

    gt -- greater than

    le -- less than equal to

    ge -- greater than equal to

    ! $var  -- not equal to

  • Scripting Session 428:10

    Scripting Session 4


    Use if condition to check wether file is exist or not


    #!/bin/bash

    if [ -f $1 ];then

    echo "File is exist"

    else

    echo "File is not exist"

    fi



    Use if condition to check wether file is exist or not


    #!/bin/bash

    if [ -d $1 ];then

    echo "Directory is exist"

    else

    echo "Directory is not exist"

    fi



    Control the input statement


    #!/bin/bash

    if [ $# -eq 2 ];then

    add=$(expr $1 + $2)

    echo "Addition is $add"

    else

    echo "Please enter two numbers"

    fi



    $# - number of argument is passed

    $? - exit state of previous command (0- sucess, non-zero - fail)

    $0 - name of shell script we have written

    $1 - first argument

    $2 - second argument



    #!/bin/bash

    tar -cvf /home/ec2-user/abc.tar /home/ec2-user &>/dev/null

    if [ $? -eq 0 ];then

    echo "Backup sucessfull"

    else

    echo "Backup fail"

    fi


    #!/bin/bash

    if [ $# -eq 1 ];then

            if [ -f $1 ];then

            echo "File is exist"

            else

            echo "File is not exist"

            fi

    else

    echo "Please enter file name"

    fi

  • Scripting Session 537:59

    Scripting Session 5

    note-  While loop is used to read line by line and for loop is used to read word by word

    Loops - It is used to write stetemt multiple times until condition is matched


    Types of loops


    While

    for

    Until


    1. While loop - It will execute condition line by line until given condition in bracket is not wrong


    while [ condition ]

    do

    statement

    done


    #!/bin/bash

    NUM=0

    read -p "Enter number to print line-->" count

    while [ $NUM -ne $count ]

    do

    echo "My name is Aaftab"

    NUM=$(expr $NUM + 1)

    done


    2. Until loop - It will execute condition line by line until given condition in bracket is  wrong


    until [ condition ]

    do

    statement

    done


    #!/bin/bash

    NUM=0

    read -p "Enter number to print line-->" count

    until [ $NUM -eq $count ]

    do

    echo "My name is Aaftab"

    NUM=$(expr $NUM + 1)

    done

    Combination of while loop with if condition using infinity loop


    #!/bin/bash

    while [ 1 == 1 ]

    do

    read -p "Enter your name -->" NAME

            if [ "$NAME" == "exit" ];then

            exit;

            else

            echo "Your name is $NAME"

            fi

    done


    To take the file input using while loop always used read


    syntax


    while read var

    do

    statement

    done < file.txt


    example

    #!/bin/bash

    while read file

    do

    echo "My name is -->" $file

    done < name.txt

  • Scripting Session 655:25

Requirements

  • Basics of Linux is requiredto understand shell scripting

Description

Have you tried to learn shell scripting on your own, but lack the structure you need to really improve your skills?

Are you tired of picking up bits and pieces of information that you can't just seem to put together?

Do you learn best by doing?

If so, you're going to love this course.

One of the biggest complaints I hear from students is that most of the courses they've taken in the past simply provide information without any context and without any idea of how to put that information to use!

This course turns that old, frustrating, and outdated way of learning on its head.

It's project-based, which means instead of learning bits and pieces of information, you'll write actual shell scripts that you can use in real-world situations. You get the chance to immediately put what you learn to use so that you fully understand and remember it.

In this Linux shell scripting course you will learn how to:

  • Name your shell scripts.

  • Use the proper permissions on your shell scripts.

  • Create and use variables in your scripts.

  • Use shell built-in commands and operating system commands.

  • Make the most out of special variables that are available to you in your scripts.

  • Make decisions by using if statements and performing several different kinds of tests

  • Check the exit statuses of commands and why you need to.

  • Use cryptographic hash functions

  • Create random data so you can do things like automatically generate strong passwords for user accounts.

  • Perform the same action or set of actions over a series of data utilizing for loops, while loops, and infinite loops.

  • Control all types of input and output.

  • Accept standard input from a user as well as from another program.

  • Redirect standard output and standard error.

  • Combine standard output and standard error

  • Use command pipelining

  • Perform text and string manipulation.

  • Process command line arguments

  • Creating functions and when to do so.

  • Parsing, analyzing, and reporting on log files, CSV files, and other data.

  • Writing scripts that execute commands on other systems.

  • Much, much, more...

When you enroll, you get lifetime access to the course. Your course never expires. You can study at your own pace and refer back to the lessons whenever you want!

I stand behind my courses and care about your goals. That's why this course comes with an unconditional 30-day money-back guarantee. It's my personal promise of your success! So...

If you're ready to level-up your shell scripting skills, enroll now!

Who this course is for:

  • Intermediate of Linux users