23 Shell Scripting
23 Shell Scripting
Linux Architecture
======================
================================
Linux Architecture Components
===============================
1) Applications / Commands
2) Shell
3) Kernel
4) Hardware components
===================
What is shell ?
===================
Note: when we execute a command, shell verify command syntax. If commad is valid
then shell will convert that command into kernel understable format.
==========================
What is Kernel in linux ?
==========================
=> Kernel will get instructions from shell then kernel will convert that command
into hardware understandable format.
=> Scripting means set of commands we are keeping in a file for execution.
whoami
pwd
date
cal
ls -l
Note: instead of executing these commands one after other manually we can keep them
inside a file and we can execute that file which is called as Scripting.
=> The process of executing script file using shell is called as shell scripting.
=> Shell scripting is used to automate our daily routine works in the project.
Ex:
a) take backup
b) delete temp files
c) analyze log files
d) system health checks
====================================================
What is difference between programming & scripting
====================================================
============================
what is sha-bang in linux ?
============================
=> sha-bang is used to specify which shell we should use to process our script
file.
syntax:
#! /bin/bash
#! /bin/bash
read NAME
#! /bin/bash
read FNAME
read LNAME
===========
Variables
===========
a=10
b=20
name=ashok
gender=male
age=20
=> The variables which are already defined and using by our system are called as
System variables.
$ echo $SHELL
$ echo $USER
$ echo $PATH
Note: We can access all the environment variables using below command
$ env
=> The variables which we are creating based on our requirement are called as 'User
Defined Variables'.
name = ashok
id = 101
age = 25
gender = male
echo $VARIABLE_NAME
# remove variable
unset variable_name
Note: If we use export command in terminal for setting variables then those
variables will be removed once we close our terminal. These are called as temporary
variables.
====================================
How to set variables permanently ?
====================================
=> We will use .bashrc file to set variables permanently for the user.
=> In user home directory, .bashrc file will be available (it is hidden file).
$ ls -la
$ cat .bashrc
# Access variables
echo $COURSE
echo $TRAINER
Note: In linux machine, every user will contain their own .bashrc file.
================================================
How to set variables for all users in linux ?
=================================================
$ cat /etc/profile
Note: If we add variables in /etc/profile then those variables applicable for all
users in linux vm.
==========================
Rules for Variables name
==========================
Ex : - (hypen), @, #
============
Operators
============
10 + 20 => 30
20 == 20 => true
========================
Arithematic Operators
========================
Addition : +
Substraction : -
Multiplication : *
Division : /
Modulas : %
$((FNUM+SNUM))
read FNUM
read SNUM
===================================
Relational/Comparision Operators
===================================
Equal : == or eq
Not Equal : !=
========================
Conditional Statements
========================
Ex :
Syntax:
if [ conditon-1 ]; then
// stmt-1
else
// stmt-3
fi
#! /bin/bash
==================================================================================
Requirement : Take a number from user and check weather it is positive or negative
or zero
#! /bin/bash
if [ $A -gt 0 ];then
echo "It is +ve num"
else
echo "It is zero"
fi
==============================================================================
Requirement : Take a number from user and check given number is even or odd
==============================================================================
##################################################################################
Date : 23-July-2024
Topic : Shell Scripting
##################################################################################
-------------------
Looping Statements
-------------------
---------
for loop
----------
#! /bin/bash
================================================
For loop example - Print Numbers from 10 to 1
================================================
#! /bin/bash
==========================================================================
Requiremnt: Write shell script to print even numbers in between 1 to 20.
==========================================================================
#! /bin/bash
=============
While Loop
=============
syntax:
while [ condition ]
do
// stms
done
------------------------------------------
print nums from 1 to 10 using while loop
------------------------------------------
#! /bin/bash
N=1
while [ $N -le 10 ]
do
echo $N
let N++
done
============================
Print Numbers from 10 to 1
============================
#! /bin/bash
N=10
while [ $N -gt 0 ]
do
echo $N
let N--
done
======================
Functions / Methods
======================
=> Using functions we can divide big task into multiple small tasks.
-------
syntax
-------
# create function
function funName(){
// stmts (body)
}
#! /bin/bash
function welcome(){
echo "welcome to ashokit"
echo "welcome to devops"
echo "welcome to aws"
}
welcome
-------------------------------------------------
========================
Command Line Arguments
========================
=> cmd args are used to supply values to script file at the time of script
execution
-------------------------------------
#! /bin/bash
echo "================="
=============================================================================
Requirement: Write shell script to perform sum of two numbers using cmd args
=============================================================================
#! /bin/bash
===============
Assignments
===============
2 * 1 = 2
2 * 2 = 4
..
2 * 10 = 20
================
CRON JOB Syntax
================
Syntax: * * * * * <script-file-name>
==================
What is CROND ?
==================
=> Every minute, CROND will be checking for CRON Jobs Schedule for the execution.
======================
Sample CRON Schedules
======================
URL : https://crontab.guru/
======================================
Where to configure cronjob in linux ?
======================================
====================
CRONJOB Practicals
====================
$ vi task.sh
touch /home/ubuntu/f1.txt
touch /home/ubuntu/f2.txt
$ chmod +x task.sh
$ crontab -e
$ ls -l
$crontab -r
=========
Summary
=========
1) Linux Architecture
2) What is shell
3) What is kernel
4) What is shell scripting & why ?
5) Proramming vs Scripting
6) Sha-bang
7) Variables (env & user-defined)
8) .bashrc file
9) cmd arguments
11) Operators
12) Conditional Stmts (if-elif-else)
13) Looping stmts (for, while)
14) functions
15) Cronjobs