Linux Class 2 Ashok It
Linux Class 2 Ashok It
What is scripting ?
======================
=> 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.
a) take backup
============================
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
#! /bin/bash
read FNAME
#! /bin/bash
read FNAME
read LNAME
====================
Scripting Concepts
====================
1) Variables
2) Operators
4) Looping Statements
6) Functions
===========
Variables
===========
Note: We can access all the environment variables using below command
$ env
=> The variables which are already defined and using by our system are called as
System variables.
=> 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.
$ ls -la
$ cat -n .bashrc
# open .bashrc
vi .bashrc
# add variables at end of the file
COURSE=devops
TRAINER=ashok
# Access variables
echo $COURSE
echo $TRAINER
Note: In linux machine, every user will contain their own .bashrc file in user home
directory.
==========================
Rules for Variables name
==========================
Ex: - (hyper), @, #
============
Operators
============
10 + 20 ==> 30
25 == 25 ==> true
========================
Arithematic Operators
========================
Addition : +
Substraction : -
Multiplication : *
Division : /
Modulas : %
#! /bin/bash
read FNUM
read SNUM
===================================
Relational/Comparision Operators
===================================
===================
Conditional Stmts
===================
Ex :
read AGE
Syntax :
if [ condition-1 ]; then
// stmts
//stmts
else
// stmts
#! /bin/bash
read AGE
========================================
Requirement : Take a number from user and check given number is even or odd
Requirement : Take a number from user and check weather it is positive or negative
or zero
=========================================