Shell Scriptng Notes
Shell Scriptng Notes
SHEBANG LINE ?
#!/bin/bash { #!-shebang , /bin/bash - line }
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
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
<<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
du -t disk utlizization
ls -lrth
==========================
VIDEO CLASS 2
==========================
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 "
variable assigmnent
======================
a=12
b=23
name= "kk funda "
with help of $ you can access any data inside that variable
example: $a
echo $a
env|grep -i "hist"
histsize=1000
echo $HISTSIZE
1000
echo $HISTSIZE
900
NAMING CONVENTIONS
======================
1. shell file name max limit is 255 character
2. dont give system defined commands
==================================================================
syntax ??
example ??
whie running a ss, if you are passing any values called as a command line arugments
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
===================
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 $@
vi sv.sh
echo $2
echo $2
echo " hero"
echo $#
echo $@
echo$*
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 $?
==============
VIDEO 4
==============
what is a loop in ss ?
-----------------------
for ex : i wrote a shell scripting
=========
FOR LOOP
========
syntax:
------
for variable in list
do
#commands to execute for each item in the list
done
ex :
vi loop.sh
#! /bin/bash
================
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 ):"
-------------
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
#! bin/bash
# Example of a while loop counting lines from a file
filename="l6.sh"
--------------------------------------------------------------------
< 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"