Friday, March 3, 2023

Shell_Scripting

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


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home