Bash Shell Script

 

Class:01

What is Shell ?
         Shell is a programm,
         It will take the commands and it will gives to the OS to perform the Commands.

         Shell is an interface b/w User and OS.

Types of Shells:
  • sh
  • bash
  • ksh
  • zsh
  • csh
How to know which shell is installed in our OS?
        /etc/shells
        cat /etc/shells
        
        By default Sh & Bash shells are installed in our OS.
        sh
        Bash

Which Shell we are using on our OS?
        echo $SHELL
        ps -p $$
        echo $0

How to change the Shell type?
        /bin/csh
        /bin/ksh
        /bin/sh
        /bin/bash

What is the default Shell Type in latest OS?
        bash

Bash Shell Scripting:
        A Shell is a file containing a series of commands.
        The Shell reads the file and carries out the commands as through  they have been entered directly                    on the command line.
        Normally the Shell Script has the file extension is (.sh).

Bash Shell Scripting--------Linux
PowerShell Scripting--------Windows   (DOS Commands).

Why we need to learn the Shell Scripting?
  • Automate the regular jobs.
  • Taking database backups.
  • Monotering Several Server resources like...
    • CPU Utilization 
    • Memory Utilization
  • Portable (It can be executed in any Unix-like OS without any modifications)
Server_Resource_Monitering.sh
Dbback.sh
Cleanup.sh
Deploy.sh

#!/bin/bash ---------->Shebang line
--
|
|
------->.Abslute path of  shell type

How to run the Shell Script?
  • ./hello.sh
  • . hello.sh
  • sh hello.sh
  • bash hello.sh
#!/bin/bash

echo "Welcome to the world.."
echo "Today date is:"
date

How to run the Shell Script in debug mode?
        sh  -x hello.sh              -------------------->-x ------>dubug mode

How to run the specfic debug in Shell Scripting?
set -x
echo "Welcome to the India.."
echo "Welcome to the USA.."
set +x

cat -n hello.sh
sh -x hello.sh

Class:02

Fine Naming Conventions:

Comments:
        Single Line Comment:  #
        Multi Line Comment:  <<Keyword


                                                                    Keyword
Variable:
        Variable is one storage location.
        where we can store the values.

Types of Variables
  1. System Defined Variables
    • How to see the System Defined variables?
      • env
      • printenv
      • printenv | cut -d "=" -f 1,2
        • SHELL
        • USER
        • PWD
        • HISTSIZE
      • echo $SHELL
      • echo ${SHELL}
  2. User Defined Variables

Class:03

Command Line Arguments:
      While executing the ShellScript, passing the values to the Shell Scripts is called Command line Arg.

echo $0 --------> Script file name

echo $1 --------> 1st arg value
echo $2 --------> 2nd arg value
echo $3 --------> 3rd arg value
echo $4 --------> 4th arg value
echo $5 --------> 5th arg value
echo $6 -------->
echo $7 -------->
echo $8 -------->
echo $9 -------->
echo ${10} ----->10th arg value

echo $* --------> Display all the arg Value --------> all arg into a one String
echo $@  -------> Display all the arg Value --------> each arg as a one string

echo $# --------> No.of Arguments.

echo $$ --------> Process ID.

echo $? --------> Previous command execution status.
                           0 = Sucellfull
                apart from 0 = failure

#!/bin/bash
echo "Commnad Line Argument demo..."
if [ $# -eq 2 ];
  then
    echo $0

    echo $1
    echo $2

    echo $$
    echo $?
    echo $#
  else
  echo "Please pass minume 2 Arguments."
  echo "Usage: sh $0 arg1 arg2 "
fi

String:

        String is a group of charactors  enclosed in a singe or double quotes. 
string_value="Hi Team My Name is ReddySekhar, I am working in IBM in Mumbai Location..."

Sub-String:

      Some poration of value in the  original String.

substring = 
echo ${string_value:20} ---> ReddySekhar, I am working in IBM in Mumbai Location...

echo ${string_value:20:11} ----------> ReddySekhar

echo ${string_value: -10} ------------> Location..
echo ${string_value: (-10)} ------------> Location..

Arithmetic Operations:

  • expr 2 + 3
  • expr 2 - 3
  • expr 2 * 3 -------------------expr 2 \ * 3
  • expr 10 / 2
  • expr 30 % 3
Write a ShellScript, accept the 2 numbers from the user, and perform the arthmetic Operations?

Class:04

Read Command:

echo "Please enter your name"
read UserName

echo "The User name is:"$UserName

What is an Array?
        
Inputs and Outputs Redirections Symbols:

>    ------>  Redirect Standard O/P
>>  ------>  Appending the Standard O/P
<    ------>  Redirect Standard I/P

How to store Std I/P & Std O/P?

sh filename.sh 1 2 >  filename.log ------------>it's redirect Std O/P only
sh filename.sh 1 2 >  filename.log 2>&1

File Descriptors:

Class:05

Class:06




Section: 001 ---------- Three Core Components of bash Scripts

  • echo $SHELL
  • shell
    • A program that interprets the commands you type in your terminal and passes them on to the operating system.
    • The purpose of shell is to make it more convenient for you to issue commands to your computer.
  • bash
    • BASH= Bourne Again Shell.
    • Created in 1979.
  1. The difference between Shell and a script?
  2. Why are we using the bash shell?
  3. The advantages that writing scripts offers?
  4. What is a bash script?
    • A file containing commands for the Bash Shell.
  5. A shell is a program that interprets commands and passes them on to the operating system?
    • True
  6. A benefit of bash is that it has a lot of features?
    • true
  7. Why should you write a script?
    • Scripts help automate tasks and save time.
  8. How to run the script?
    • ./our_Script.sh
    • our_Script
      • exit statement
      • sucessfull       = 0
      • un-sucessfull  =  1-255
  9. Permission to the files?
    • [owner] [group] [other]

System path:
  • Adding scripts to your PATH
  • The PATH variable tells the shell where to search for executable files.
  • We can add folders to our PATH variable by modifying the .profile file.
  • We can make our scripts accessible system-wide by putting them into a folder covered by our PATH.
    • echo $PATH
      • vi .profile
      • export PATH= “$PATH:$HOME/bash/Scripts”
      • source ~/.profile
Section: 002 ---------- Variables
  • Variables allow to us to store useful data under convenient names.
  • Shell expansions are very powerful features that allow us to retrieve data, process command output and perform calculations.
Parameter:
  • A parameter is any entity that stores values.
  • Types of parameters:
    • Variables
    • Positional parameters
    • Specific parameters
      • Variables:
        • A parameter whose values you can manually change.
        • Creating a Variables
          • name=value (no space around= sign!)
          • Retriving a value: ${parameter}
        • shell varaibles:
          • A shell Variables also known as environmental variables.
          • PATH
          • HOME ( Absolute path to the current user home directory )
          • USER
          • PS1
          • HOSTNAME
          • HOSTTYPE  (What type of processor architecture the computer is running.)
            • The HOME and USER variable tell you about the user who is logged in.
            • The HOSTNAME and HOSTTYPE variable tell you about the computer they are using.
    • If you want to change the first letter of your variable into lower case ?
      • echo ${name,}
      • echo ${name,,}
    • If you want to change the first letter of your variable into Upper case?
      • echo ${name^}
      • echo ${name^^}
    • how to find How many characters has variable?
      • echo ${#name}
  • Sub-string expansion / slicing:
    • numbers=0123456789
    • ${parameters: offset: length}
      • ${number: 0:7}
      • ${number: 1:5}
      • ${number: 4}
      • ${number: 4:}
        • ${number: -3:2}
  1. What is the difference between $parameter and ${parameter} ?
    • ${parameter}
      • Is the standard syntax for parameter expansion and allows for advanced expansions.
    • $parameter
      • Is the simpler syntax but does not support advanced expansions.
  2. How would you create a variable with the name “Sam” stored in it?
    • name=Sam
  3. What does ${#parameter} do?
    • Tells us how many characters the parameter’s value contains.
  4. Which shell variable will give you a parameter’s value, but show it in all uppercase?
    • ${parameters^^}
  5. Which shell variable contains the directories that the shell should search for executable files?
    • $PATH
    • ${PATH}
  6. What information does the $USER shell variable contain?
    • The username of the current user
Command Substitution:
  • Use command substitution to save the output of commands in variables.
  • Use command substitution to use the output of one command inside another command.
  • Command substitution is a shell feature that allows you to grab the output of a command and do stuff with it.
  • Substitution_Script.sh
  • Command Substitution syntax: $(command)
Arithmetic Expransion:
  • The syntax for arithmetic expransion is:  $((expresssion))
  • Arithmetic expansion uses an order of precedence when handling operators to get the right result.
  • Arithmetic expansion unable to handling decimal number.

Dealing with decimal numbers:
  1. what is bc command?
    • bc stands for basic calculator.
    • bc command allows us to perform calculations with decimal numbers.
    • The syntax for the bc command is: echo "expression" | bc
    • use the scale variable to control the numbers of decimal places that are included in the output.
    • bc (type in terminal)
      • echo "5/2" | bc
      • echo "scale=2; 5/2" | bc
      • echo "scale=5; 5/2" | bc
  2. How would you calculate 5 to the power of 3 using arithmetic expansion?
    • $(( 5**3 ))
  3. You would use the bc command instead of arithmetic expansion when you need your output to include decimal places?
    • true
  4. What is the purpose of the scale variable in the bc command?
    • To set the numner of decimal places to be included in the out
Tilde Expansion:
  • It will indicate current user home directory
  • Tilde expansion within the shell is useful when writing scripts that need to work across multiple directories.
  • echo ~
  • echo ~colleague 
  • echo ~root
    • echo $PWD ---------- cd ~+
    • echo $OLDPWD ------- cd ~-
  1. ~ enables us to refer to the current user's home directory without having to type out the full path?
    • True
  2. ~+ expands to the value stored in which shell variable?
    • PWD
  3. ~- will change back to the previous active directory?
    • True
Brace Expansion:
  • There are two types of brace expansion:
    • string lists: {1,2,3,sarah,a,b,c}
    • Range lists: {1..1000}
  • echo {1,2,3,sarah,a,b,c}
  • echo {jan,feb,mar,april,may,jun}
  • echo {1,2,3,4,5,6,7,8,9,10}
  • echo {1..10}
  • echo {10..1}
  • echo {a..z}
  • echo {1..100}
  • echo {1..100..3}
  • echo {1..100..5}
  • echo {1..100..10}

  • echo month{1..12}
  • echo month{01..12}
  • mkdir month{1..12}

  • echo month{01..12}/day{01..31}.txt
  • touch month{01..12}/day{01..31}.txt
  1. {1..5} would expand to how many values?
    • 5
  2. What expansion would we get from {1..10..2}?
    • 1 3 5 7 9
  3. How would we run two expansions within one command line?
    • touch ~/Folder{01..3}/File{01..10}

Section: 003 ---------- 
Section: 004 ----------
Section: 005 ----------  
Section: 003 ---------- 
Section: 004 ----------
Section: 005 ----------  
Section: 003 ---------- 
Section: 004 ----------
Section: 005 ----------  














Comments

Popular posts from this blog

AWS

Linux

kubernetes