0% found this document useful (0 votes)
8 views12 pages

Shell Scriptng Notes

The document provides a comprehensive overview of Linux shell scripting, covering various types of shells, installation methods, and how to check and switch between them. It explains shell scripting fundamentals, including writing scripts, executing them, debugging, and the use of comments and variables. Additionally, it discusses command line arguments, special variables, and control structures such as loops in shell scripting.

Uploaded by

chinnakumark8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Shell Scriptng Notes

The document provides a comprehensive overview of Linux shell scripting, covering various types of shells, installation methods, and how to check and switch between them. It explains shell scripting fundamentals, including writing scripts, executing them, debugging, and the use of comments and variables. Additionally, it discusses command line arguments, special variables, and control structures such as loops in shell scripting.

Uploaded by

chinnakumark8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

linux /// shell scripting

default shell [bash bourne ]


there are different types of shell
korn shell
bash shell
c shell
z shell
ksh
how many shells are their in linux servers or what are shells confirugred in
linux servers
cat /etc/shells
how to install
sudo yum install csh -y
how to see
cat /etc/shells
installed or not
sudo yum install -y zsh [ best prctice ]
sudo yum install -y ksh

how to check which shell we are using


echo $shell (echo -- print )
echo $0
ps -p $$ ( by default process check cheya daniki ) backend ey process is
running
env ( here shell will be there with help of grep )
env | grep
how to switch to another shell ??
step 1 : cat /etc/shells
check what shells are there
step 2 : echo $shell
current shell ?
/bin/sh
sh-5.ls echo
echo $shell (bydefault shell check cheyadaniki )
/bin/bash
if you want to come back
/bin/bash
ps -p $$

what is shell scripting ??


shell is a file its contains collections of linux commands , whatever the order you
provided
will be execute from top to bottom
what is the extention of shell scripting ??
ans: .sh {( optional ) as a good pracitce pls give .sh to identify }
why i need to learn to shell scripting ??
where ever you doing manual task and repated task . to autmate the manual tasks
SHELL SCRIPTIING IS ONLY FOR DEVOPS ??
devpoler / tester / devops
ans : no and is all for engineers
ex : db backup / application backup / cpu memory check cheyli / server
utilization.sh
what are the prequisite to learn SS ??
1 linux commands
2 basic programming fundamentals
3 commitment
4 problem sloving skills
how to write a shell scripting ??
vi first.sh
go to insert mode and insert data
echo " i am from hyd"
echo "how are you "
date
pwd
wq!
i want to see output

how to run a shell scripting ?


we can run in 4 types
sh first.sh
./first.sh
. first.sh
bash first.sh
ls -lrth

sh first.sh { TO RUN SHELL SCRIPTING }

{ as a good practice what other developer are using }


./first.sh
permission is denied execution permission is not their
ls -lrth|grep -i "fir"
not their permission execute
how to give excute permission for that user
chmod u+x first.sh
rerun the command
ls -lrth|grep -i "fir"
./first.sh

this is script work in windows ?


no
linux is a platform dependent os ??
linux === we can write shell script
windows server == power shell
python == platform independent

sh -x first.sh { debug mode -x debug , first.sh - file name }


each command
output
How to run a script in debug mode ?? *** real time
sh -x first.sh

IN cat file i want to debug for partucular command UPTIME


go to
vi first.sh
date
pwd
hero
hello
set -x starting point
uptime
set +x ending point
date
chicha
now run the file
sh first.sh
EX :I want to run debug for the pwd and uptime command
vi first.sh
hero
set -x first.sh
pwd
set +x first.sh
zero
set -x first.sh
uptime
set +x first.skh
chinna

SHEBANG LINE ?
#!/bin/bash { #!-shebang , /bin/bash - line }

COMMENTS IN SHELL SCRIPTING ??


=============================
-- > comments are used to improve the readability of script
--> It will stop the execution of a few or more commands

types of comments
================
1 single line comments
2 inline comment
3 multi comment

vi first.sh
hero
hell
#hello i am displaying the print working dir { aa command guruchi describe chesham
alagane ey command mundu describe cheyali } {# -single line comment & # that is
ignore by shell }
chicha
red
green
{ nenu oka script rashanu.oka six months tharuvatha aa company nunchi vellipoyanu
aa script vere resource or person echinappudu handle cheyali kadha
eppude aite 10 coomands unthadi real time lo 100 / 1000 /2000 undachu code commetns
unte oh e line lo user add cheshadu

./sh first.sh
outout displayed
comments ingored
comments will not come here . comments enduku anthe a code chadiviteappudu it
will increase readability of script
suppose i want to run the code 1 2 3 4 working and i dont uptime
#uptime { it will stop execution of few commands }
if you place # it will not run that line
now run that file
./first.sh
no display of uptime

WHAT IS INLNE COMMENTS ??


========================
appudu aite ni dagira code undoo
vi first.sh
hero
man
kill
echo "hero today coming to shoot " #here display code { inline comments means it
will nor start from first that will start in the middle that is called comment }
{in the middle of the line we are using IC }
zero
fill
vbn
wq!
./first.sh { running output }
ingorable
that is also not displayed

MULTI LINE COMMENTS *** INTERVAL QUESTION


===========================================
the way of writing coding is important.and should understandable by opposite person

this is a code

#hero coming
#echo " how are you "
#date this is a nice
the boy #coming to home
uptime
hero
ls
cd
lsw

ex : realtime
task1
100 line
task2
100 150 lines
task3
script run cheychinappudu task 1 2 3 vasthadi
na requirement enti ante
nenu test cheychiappudu naku task2 output vadhu 5150 lines vadhu
{enti nuvu code delete chesthava never delete a code} {keep that code like that
only}
keep multi line comments for task 2 it will skip and how will you do

vi c.sh
echo " i am good "
echo "hi i am hero "
lws
do
<<phani { multi line comments}
pwd
ls
cd
phani {end }
mkdir
jaffa

cat -n c.sh
./ c.sh
chmod u+x c.sh
./c.sh

In java / terraform/ jenkins grovvy


======================================
// single line
/*
multi line comment
*/

IN Realtime Multiline comments in shell were we use


script rashanu annuko
vi c.SH
#! /bin/bash {optional}

<<comment
author:kkphani
aim: to run the commands { comments are ignored by the shell . comments lopala
em ayna rasuko matter. ante ni code readabiltiy kosham comments
start: 10/02/0222
comment

echo "the king is back"


echo "field is back"
HERO
ZERO
PIWAN
JAFFA
WER
CHICHA
wq! save

nw we are in fundamental stage only

du -t disk utlizization
ls -lrth

requirement enti ante


sleep command
ls

==========================
VIDEO CLASS 2
==========================

VARIABLES IN SHELL SCRIPTING ????


===============================
EX : inside a memory their is a four values.
v1 = 10 v2= 25 v3=25 v4=10
which one should i call v1 or both are same values so varibales came into picture

what is a variable ??
variable is a name of a memory loctions. It is used to store a values .
the value can be changed during the program { script }execution
a= 10 b=20 c=25 d =10

{{ In the middle of a shell scripting you can change value from 10 to 15 also.
while executing the script the value can change { arthametic operations /
relational opertions or some other opertions } }}

variable assigment
==================
assinging value to variable
12 is assingned to a
a <--> varibale
= <--> assignment operator { RHS=LHS}
12 <--> is a value

a=12
b=23
name= "kk phani "

12=12 NOT RECOMENDED

RULES FOR DECLARING A VARIABLE ??


==================================
1. varilable name contains collecions of letter ( a-z,A-Z, digits (0-9) and under
score ( _)
ex:
name (yes)
%6#a=90(no)
specical charcter not allowed expect under score _
2. variable names doesnot start with no
ex: 25=999 (no)
3. varibale can start with either letter or under score ( _) but not digit
ex: _abc=89 (yes )
2a_=999 (no )
4. space are not allowed in vaiables name
ab de=99 (no)
5. variable names are case sensitive (name is different NAME )

variable assigmnent
======================
a=12
b=23
name= "kk funda "

how to access variable values ??


================================
ans : use the $ symbol or use $ followed by variable name

with help of $ you can access any data inside that variable
example: $a
echo $a

Types of variables in shell scripting


========================================
1. system defined variables ( enviromental variable )
2. programmer ( user) defined varibales

1: System defined variables ( enviromental variable )


By default unthai no need to write variable
Varibales are inherited from shell environment variables is called a system defined
variables

how many system defined varibales are there in linux ??


==========================================================
------> env or printenv
ex: echo $user
echo $HISTSIZE

By default how many variables are there


env|wc -l
system varibales names ( upper case lo unthai )
echo $user
ec2-user
what is HIST SIZE ??? inv question
====================================
ans: 1000 bydefault

env|grep -i "hist"
histsize=1000

what is the meaning of HIST ??


hitory run chesthav ga command . history run chechinappudu
ani commands nuvu history lo store chesukovali
bydefault 1000
eppudu nuvu ani run cheshav commans 400
old one erase aipothadi that meaning of history

echo $HISTSIZE
1000

IS it possible to change histsize ???


===================================
export HISTSIZE=400

echo $HISTSIZE
900

2: programmer ( user) defined varibales


=======================================
user defined varibales
EX:
age=90
def =89
name= "phani"
amount=2000

NAMING CONVENTIONS
======================
1. shell file name max limit is 255 character
2. dont give system defined commands
==================================================================

syntax ??

varibalename= <value> { this a syntax , oka obstract }

example ??

name= "krishna" { syntax based chesukoni example rasthunam }


=======================================================================

COMMAND LINE ARGUMENTS IN SHELL SCRIPTING ??


=================================================

whie running a ss, if you are passing any values called as a command line arugments

EX :sh test.sh one 10 20 sai 30


while running a script after that you are passing multiple arguments with
separately space this values is called as command line arguments
OR
when you are running a script the arugments you are passing is called command line
arugments

echo $0 -------> file name of the script


echo $1 ------> first argument
echo $2 ------> second argument
echo $3 ------> third argument

where using this in real time ??


================================
ex: writing script for backup. what is the source loctions

sh back.sh /temp/

backup_loction=$1

===========================================================

vi commandline.sh
echo $0
sh commandline.sh

vi commandline.sh
echo $0
echo $1
echo $2
sh commandline.sh
commandline.sh

two lines came empty . if we are passing data it will display . if we are not
passing data it will not display

sh commandline.sh one java


commandline.sh
one
java

whatever the value while running a script. whenever running a script


after the script whatever value you are passing that will be stored in commandline
variables $1 $2

I want to get a 11th arugmment

echo ${11} ---> it will display the 11th values

===================
SPECIAL VARIABLE
====================
$# -----> it will give the no of arugmnents you passed from the command prompt
or
$# ----> total no of arguments passed through script (count)

vi sv.sh
echov $1
echo "welcome to kk funda"
ech0 $2
echo $4
echo $3
sh sv.sh one sai kk funda
one
kk funda
sai
fuda
kk
==================================================
how many argumnet passing in cli. i want to know
=======================================================
with help of $# it will give the no of arugments passed through that cli
one
kk
sai
funda
4

$@ -----> It will display all values at a time { "one" "kk" "gh" "hJ" we have 4
string } it will run in backend

vi sv.sh
echo $!
echo $2
echo $#
echo $@

$*------>it will display all values at a time as string "one dd gg jj""

vi sv.sh
echo $2
echo $2
echo " hero"
echo $#
echo $@
echo$*

$$ ----> it will give processes id

vi sh.sh
echo $1
echo $2
echo $@
echo $#
echo $*
echo $$

sh sv.sh one kk hg pg
sai
funda
kk
3
6233

ps -ef
$? -----> ( important) what is the status of previous command
0----> success
127 -----> fail
vi sv.sh
echo $1
echo $@
echo $#
echo $%
echo $?

run the code


sh.sv.sh one sai kk funda

==============
VIDEO 4
==============

what is a loop in ss ?
-----------------------
for ex : i wrote a shell scripting

Loop is a statement it will repeat the same statement n number of times

=========
FOR LOOP
========
syntax:
------
for variable in list
do
#commands to execute for each item in the list
done

ex :
vi loop.sh
#! /bin/bash

# example of a for loop ilterating over a list of numbers


for num in 1 2 4 5
do in
echo number: $num
done
sh loop.sh

================
NESTED FOR LOOP ***
===============
IF any loop this is the for-loop within other-loop
outer for-loop
inner for-loop
first outer for-loop nunchi oka input thisukunthadi.aa input ni entire inner for-
loop repeat chesthadi

#! /bin/bash
# example of nested for loops to generate a multiplication table
echo "multiplication table (1 to5 ):"

for (( i=1;i<=3; i++))


do
echo "mulitiplication tabld for $!:"
for (( j=i; j<3; j++))
do
echo "$! * $j = $((i*j))"
done
echo " -----------------------"
done

j++ ----> post increment


++j ----> pre increment
j-- ---> post decrement
--j ---> pre decrement

-------------
WHILE LOOP
-----------
The while loop executes a block of code as long as a specified condition is true

syntax
while [ condition];
do
#code block to execute as long as condition is true
done

ex:

#! bin/bash
# Example of a while loop counting down from 5 to 1
counter=5

echo " counting down "


while [ $counter -gt 0]
do
echo "$counter"
(( counter--))
done
echo " you are good engineer"

example 2 : reading lines from a file


-----------

#! bin/bash
# Example of a while loop counting lines from a file
filename="l6.sh"

echo " content of file "


while IFS= read -r line #IFS:internal filed separator
do
echo "$line"
done < "$line"
echo " you are great"

--------------------------------------------------------------------
< less than ---- aa file unna vathi ni input ga pass chesthav daniki
<< ----- append
----------------------------------------------------------------------

------------------------------------------
example 3: processing user input unit exit
-------------------------------------------

#! bin/bash
# Example of a while loop processing user input untill exit is entered
echo "enter names (type 'exit' to quit):"

while:
do
read -p "name:" name
if [ "$name" = "exi" ]; then
break
done < "$line"
echo " you are great"

You might also like