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

Shell Scripting Beginner Notes

The document provides an overview of shell scripting, covering topics such as basic scripting skills, conditional statements, loops, and functions. It includes practical examples and explanations of how to set up a shell environment, write scripts, and handle user input. Additionally, it discusses advanced techniques and real-world applications, particularly in the context of DevOps and automation.

Uploaded by

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

Shell Scripting Beginner Notes

The document provides an overview of shell scripting, covering topics such as basic scripting skills, conditional statements, loops, and functions. It includes practical examples and explanations of how to set up a shell environment, write scripts, and handle user input. Additionally, it discusses advanced techniques and real-world applications, particularly in the context of DevOps and automation.

Uploaded by

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

Si "

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 .

Real World and


>
-
Application >
- Docker
integratio
Integration
setuppinga
>
-

>
- 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

Apps S WE ARE LEARNING

Shel > Interaction


Linux Kernel-
= "C"programming
Architecture #Hardware

> We linux shell commands.


operate using
-

on

Example : We are
printing something
,
shell command is Echo

es There are a lot of commands to


linux
communicate with
operating system .

>
- /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 .

cat hello .+ >


-
"Hello Abdur"

Shell Script >


- shell script extension "Sh"

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 .

echo Derops" Is-1 > To check


permission .

echo "Abdur : create ↓ If will be read 75


a
adirectory" only (Not Executable)
v
Me
and write
echo
- Now to make it executable,
mkdir sample .

run >
- chmod 759 hello Sh
,

↓ hello Sh . is now executable-



Run - of hello -
Sh
Basic Scriping Skills .

Conditionals loops
variables
, Arguments , ,

Functions . for while

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.

Script file >


- chmod 755 file-name She .

To run Script-file-name . Sh

*
Name is jetha output

date-
To print
* >
-
-

TAKE USER INPUT

# ! /bin I bash >


- / script . Sh
"
echo "enter the name : enter the name :

read username jetha

E echo "you entered username you entered jetha.


S
ARGUMENTS Argument I

filsh Argument
>
-
We can use these
arguments
in a
script or
Code.

I
filish t To print these
argument
·


let's make some

only (5) first argument will


changes in the script
.

run >
- Because
of no Access . Script
..

echo "the character in

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

= ScriptTo check if-else.


else Example #! /binI bash

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 <

if num is greater than

"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

conditional when there


-> are
multiple diff choices
.

so rather
using a
few if and if-else
statements ,
we can use a
single case statement .

SYNTAX :

① statements start with


case
Isome-variable in All case

"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

e executed in case that the


pattern
matches the variable or
expression
(variable + Carl !
>
-
that is
specified
-
(age1) @ All clauses have to be ferminated

Kage2) by adding ";;" at the end .



-

you can have a


default statement
(case])
by adding "asthepatterna
a

= left) "esac"

keyword .
(case type
Backward
BASH Loops :

used
*
loops are
for
Automating repetitive tasks

Iterating through lists or


sequences
Executing commands multiple times

based on conditions.

For Loops :

* Used
for iterating over a series
of
values

or
performing repetitive tasks

Structure OFA For loop Example


# 1binI bash
$Elist
!

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

read-p "What is name" name


your

While [[-z BEname B]


do
" name
echo cannot be blank
-
> user will
prompt to enter
Please Enter your name
name

read-p"Enter your name again" name e


if left blank

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 :

With AWS CloudWatch


Monitoring
-
FUNCTIONS
functions are
great way to reuse code.

for example
to
:

if we want to check the sum


of
numbers time when the .
gives input
every user

do not the code


We
everytime
write same

for the sum .

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

And Accessing Variab


are

Example variable a

1st
>
- input

>
- input
2nd

>
- function call .

You might also like