Shell Scripting Beginner Notes
Shell Scripting Beginner Notes
Content
-
:
cripting
>
- Introduction
>
- Variable
>
-
Arguments
>
- Conditional Statements
>
- loops 11-October
>
- functions . 12024)
ogling
Shell
>
Scripting used for
-
-
>
auformation
L
for DevOps -
↓
for
Roadmap :
-Terminalsdirectlywithrepetiti
interactive
tasks ,
>
- Introduction to shell >
- Terminal setup
and Environment
Setup >
- vim , nano etc
>
- Basic Scripting Skills
>
- variables ,
conditions functions
,
>
- operators , loops
- Intermediate
Scripting Techniques
>
- Errors
> Email
- - handlingfailure
when occur .
>
- Backup restore of databases
>
- Gone -> schedule .
>
- Real world Application and Integration
Shell and learning
Mastery
>
- continous
.
>
- Advanced Scripting and
Debugging
>
-
Reading logs
Awk sea
>
- Automate
, ,
grep
using
python language .
>
- Shell Mastery updating kas
>
- continous learning nodes
for Industry. Autoscale
Project
>
- >
-
.
shell script
code Github
↓
SSH to ECL
I
c om
Permissions
ps.
S e vo
qd
Docker
z za
docker run ra
↓
Image a
container
formation.
- logs Analysis .
Invite
(shell scripting)
INTRODUCTION
TO SHELL
AND
ENVIRONEN
·
Linux OS
Linux Kernel-
= "C"programming
Architecture #Hardware
on
Example : We are
printing something
,
shell command is Echo
>
- /bin/sh >
-
path.
Temprovement in Shell
(scripts)
/bin/bash used !
->
mostly
To >
- First need
script
we an
>
- run shell
environment >
- can be
provided by multiple
cloud platforms .
EC2 Where will
>
- Create an instance >
- we run
shee scripts.
I
vim ,
vi >
-
connect with it
L
Nano etc => Install Text Editor to write
Shell
a
Script. hello xt
"Insert Exit
.
To
Hello Abdur
:ga)
Mode
Long
(Type'i) Tr
,
↓ write ↑ a
Equit quitwithout e
>
-
To
To
create a
what's
new
in
fileim
the
helloxt -
(writesomethinga
>
- veiw
file .
create shell
-
>
- a
script name
"hello" -
> vim hello . Sh
#! /bin/bash >
-
Tells interpreter which
type of
# This is Sample Script
shee it
is--which bash
fr
echo "Helle Abdar" It will tell you
>
- Now if you do
"Hello the bash path .
run >
- chmod 759 hello Sh
,
Conditionals loops
variables
, Arguments , ,
VARIABLES
line comment
#
single
# comment
(Block)
Example
- Anything
Anything written
written here >
- are commented !
here >
- are commented !
.
comment
(Block)
line comment
Single
-
Multi-line Comment
>
9
variable
-
- S- dollar sign use
for identification of variable.
To run Script-file-name . Sh
*
Name is jetha output
date-
To print
* >
-
-
filsh Argument
>
-
We can use these
arguments
in a
script or
Code.
↓
I
filish t To print these
argument
·
↓
let's make some
run >
- Because
of no Access . Script
..
output
file file 1
-
A
90
file't
·
wq
We
>
- can create multiple users
using .
arguments
Example
:
- I create Sh o
the
user
ided
· .
-
as
!pinIbash
condition
#
>
sername
-
if [-z"$1"] then
; not
echo
"
provide a username"
exit I ~> username provided !
Exit successfully
fi
userade $m > Add user .
sudo
run -
>create user Razzag
"username
Argument passed
11
provided
-
CONDITIONAL STATEMENTS (iFelse)
Book-content
if [condition :
then
8
↑
~
I
-
b then
chmod 755 numerish
2- else
↓ run the Script Fi
· I number : sh
Enter number
number is Even
-> -
condition
5 - number is odd.
2 Multiple Conditions
Scenario)
input from user <
"Positive"
zero >
- So
-
if number is less than
↓
11
zero > So
-
"Negative
(SYNAM Nor Positive ,
if Nor Negative
[l condition]]
- ↓ is
- Number
Zero
Fi
.
=
SWITCH STATEMENTS
As in other
programming
statements to
simplify complex
We use case
so rather
using a
few if and if-else
statements ,
we can use a
single case statement .
SYNTAX :
"case" .
pattern 1) keyword
commands On the same line as the
e "Case"
keyword - you need to
specify
patterne pattern-3) "variable"
a or an
expression followed
"in" .
commands by keyword
*
g
default commands ②you canspecify multiple Pate is
es al ②
After the
pattern , you specify the
Example user input
.
commands that
you
like to be
= left) "esac"
↓
keyword .
(case type
Backward
BASH Loops :
used
*
loops are
for
Automating repetitive tasks
based on conditions.
For Loops :
* Used
for iterating over a series
of
values
or
performing repetitive tasks
for var in
do
users= "Abdur Razzag Khan"
your-commands
For user in $cusers'
done
do g
echo "Basers3"
done
Example 2
# ! / binI bash
for num in
51 ...
103
do
echo $Enum3
done .
While Loop
Executes commands
a set
of as
long
as a condition is true.
Structure While loop
of 1 Example 1S Cessthan
While
do
[condition] #!
counter
/ binI bash
=
↑
your-commands while [Cscounter -
10 10]]
done do
echo counter
&( counter + +
1)
2
Example done -
#! / bin I bash
done E
echo "Hi there $ [nameB"
It will
continuously
ask enter
again
to
↓ ↓
exits until name is
When loop
It will
prompt give Exit loop ,
=
!
"
"It there
SHELL SCRIPT FOR Dep
EXAMPLE #1
Example 2
Interacting
With APIs
Example 3 :
for example
to
:
so
basically ,
we will write
function()
which each time
we can call user
gives
input *
·
required
unction
code
contains
for
all the
sum
Syntax :
omit the
>
-
we can also
"function" keyword at
start. .
work
It will
Example variable a
1st
>
- input
>
- input
2nd
>
- function call .