SlideShare una empresa de Scribd logo
1 de 24
Descargar para leer sin conexión
Fundamental of Linux
&
Shell Programming

Presented By:-Rahul Hada
hada.rahul@gmail.com
Mob:- 9001806370
RoadMap
●

Basic of shell programming
–
–

Using variables

–

Pipes

–

Performing math

–
●

Creation of shell program

Handling user input

Control Structure
–
–

if-then-else
Loops
●

●

For , while , until

Creating Function

18/12/13

OpenLab

2
Basic of Shell Programming
●

Creating a shell program
–

Specify the shell you are using in the first line

–

#!/bin/bash , #!/bin/ksh etc.

–

Ex: Shell program to print something
#!/bin/bash
echo “Welcome to the world of Shell Programming”

●

Execution of program
–

Shell script never compile

–

First change mode then execute

–

cs@poornima$ chmod 775 hello.sh or chmod u+x

–

cs@poornima$ ./hello.sh

18/12/13

OpenLab

3
Basic of Shell Programming
●

Variables – we can access the value of
variable using $ sign.
–

Types of variables
●

●
●

18/12/13

Environment Variables – stores specific system
information. ex. $HOME,$SHELL,$LANGUAGE,
$HOSTNAME,$HOSTTYPE etc
User Variables – stores our own values in a variable.
Ex:
#!/bin/bash
|
#!/bin/bash
a=5
|
read a
echo “Value of a=$a” |
echo “Value of a=$a”
OpenLab

4
Basic of Shell Programming
●

Backtick (` `)
–

It allow you to assign the output of a shell
command to a variable

–

Ex:#!/bin/bash

18/12/13

| #!/bin/bash

| d=`date`

| echo date

| echo $d

date

| #!/bin/bash

|

OpenLab

5
Basic of Shell Programming
●

Pipes (|)
–

The output of one command is the input of the
other command.

–

Command1 | command2

–

grep poornima history.txt | wc -l

–

Ex:
#!/bin/bash
a=`grep poornima history | wc -l`
echo “Number of Lines Containing Pattern=$a”

18/12/13

OpenLab

6
Basic of Shell Programming
●

Evaluating expression
–

Two ways to evaluate
●
●
●

Using expr expression
Using $[ expression]
They work differently for multiplication ( * )

|

#!/bin/bash

|

b=5

c=6

|

c=6

a=`expr $b + $c`

|

a=$[$b +$c]

echo “Sum =$a”
18/12/13

#!/bin/bash
b=5

–

|

echo “Sum=$a”
OpenLab

7
Basic of Shell Programming
●

Exiting the script
Every command runs in the shell uses an exit status.

●

Whose value vary from 0-255

●

Value stores on ? Or $?

●

Explicit exit status from script

●

cs@poornima$ echo $?
#!/bin/bash

#!/bin/bash

| #!/bin/bash

hello

|

ls -ytr

| ls -l

echo $?
18/12/13

|
|

echo $?

| echo $?
OpenLab

8
Basic of Shell Programming
●

Exit Status Codes
–

0

-------- successful completion of the command

–

2

-------- misuse of shell commands

–

127 -------- command not found

–

130 -------- command terminated with Clt-C
example using program

18/12/13

OpenLab

9
Basic of Shell Programming
●

Command line Parameters
–

It allow you to add data values to the command
line when you execute the script.

–

Ex : cs@poornima$./sum.sh 23 56

–

Shell uses special variable, called positional
parameters.

–

Represented from 0 – 9 i.e $0 - $9
#!/bin/bash
s=`expr $1 + $2`
echo “Sum=$s”

18/12/13

OpenLab

10
Basic of Shell Programming
●

Some special variables releated to the command
line parameters
–

$* , $@ , $#
#!/bin/bash

#!/bin/bash

|

#!/bin/bash

echo $#
–

|
|

echo $*

|

echo $@

Shift command – it downgrades each parameter
variable one position by default.
#!/bin/bash

./cli1.sh 3 4 5

shift
shift
18/12/13

echo $1

OpenLab

11
Control Structure
●

If-then-else
testing of if condition using test & [ ]
Structure

Example

if command

| #!/bin/bash

then

| if test $1 -gt $2

command
fi

| then
| echo “First CL is greater”
| else
| echo “Second CL is greater”

18/12/13

OpenLab

12
Control Structure
●

Test of numeric comparisons
–

n1 -gt n2

–

n1 -eq n2 -- check if n1 is equal to n2

–

n1 -lt n2

-- check if n1 is lesser then n2

–

n1 -le n2

-- check if n1 is lesser or equal to n2

–

n1 -ge n2 -- check if n1 is greater or equal to n2

–

n1 -ne n2 -- check if n1 is not equal to n2

18/12/13

-- check if n1 is greater then n2

OpenLab

13
Control Structure
●

Test of string comparisons
–

str1 = srt2 -- check if str1 is the same as str2

–

str1 != str2 – check if str1 is not equal to str2

–

str1 > str2 – check if str1 is greater then str2

–

str1 < str2 – check is str1 is lesser then str2

–

-n str1--check if str1 has a length greater then zero

–

-z str1-- check if str1 has length is zero

Note : Must use escape () symbol while
using > or <
18/12/13

OpenLab

14
Control Structure
●

Test file comparisons
–

-d file -- check if file exist and is a directory

–

-e file -- check if file exist

–

-f file -- check if file exist and is a file

–

-r file -- check if file exist and is readable

–

-w file – check if file exist and is writable

–

-x file – check if file exist and is executable

–

and, few more

18/12/13

OpenLab

15
Control Structure
●

Case Statement
case variable in

|
|

pattern3)
command ;;

case $ch in

|

command;;

read ch

|

pattern1 | pattern2)

#!/bin/bash

a|e|i|o|u)

|
|

*)
command;;

echo “char. is vowel”
;;

|

esac

*)
echo ”char is not vowel”

|
18/12/13

|

;;

|

esac

OpenLab

16
Control Structure
●

Loops types in shell for , while , until

for var in list
do
command
done
●

Different ways to represent for loop

●

Can redirect the output of for loop in file

●

For loop can take input from file

18/12/13

OpenLab

17
Control Structure
●

While Loop
while test condition
do
commands
done

18/12/13

OpenLab

18
Control Structure
●

Untile loop – works opposite way of while loop
until test commands
do
commands
done

18/12/13

OpenLab

19
Guess output ?
#!/bin/bash

| #!/bin/bash

for var in” $*”

| for var in “$@”

do

| do

echo “Output=$var” | echo “Output=$var”
done

18/12/13

|done

OpenLab

20
Creation of function
●

Function – reuse of same shell code

●

In shell function is a mini-script

●

Creation of function by two ways
function name {
commands
}

|

name () {

|

commands

|

}

|
18/12/13

OpenLab

21
Creation of Function
●
●

Passing parameters in function
Returing values from function can be three
types :–

By Default it return exit status of the last
executed command in the function

–

We can also use return to modify the exit status
as per our own requirement

–

Using echo to return values

18/12/13

OpenLab

22
Creation of Function
●

Passing array in function

●

Decleration of array
Ex:- myarray = (1 2 3 4 5)
access values of array -- ${myarray[$1]} index 1
${myarray[*]} all array
–

18/12/13

Example FunArrVar.sh

OpenLab

23
Thank You

18/12/13

OpenLab

24

Más contenido relacionado

La actualidad más candente

introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack monad bobo
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programmingsudhir singh yadav
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in LinuxAnu Chaudhry
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Scriptstudent
 
IPTABLES_linux_Firewall_Administration (1).pdf
IPTABLES_linux_Firewall_Administration (1).pdfIPTABLES_linux_Firewall_Administration (1).pdf
IPTABLES_linux_Firewall_Administration (1).pdfmpassword
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell scriptBhavesh Padharia
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scriptingVIKAS TIWARI
 
Netcat 101 by-mahesh-beema
Netcat 101 by-mahesh-beemaNetcat 101 by-mahesh-beema
Netcat 101 by-mahesh-beemaRaghunath G
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqRuben Tan
 
Linux Knowledge Transfer
Linux Knowledge TransferLinux Knowledge Transfer
Linux Knowledge TransferTapio Vaattanen
 
Kernel bug hunting
Kernel bug huntingKernel bug hunting
Kernel bug huntingAndrea Righi
 
Basic linux commands
Basic linux commands Basic linux commands
Basic linux commands Raghav Arora
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell ScriptingMustafa Qasim
 
Leveraging zeromq for node.js
Leveraging zeromq for node.jsLeveraging zeromq for node.js
Leveraging zeromq for node.jsRuben Tan
 

La actualidad más candente (20)

introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programming
 
Ns3
Ns3Ns3
Ns3
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Script
 
IPTABLES_linux_Firewall_Administration (1).pdf
IPTABLES_linux_Firewall_Administration (1).pdfIPTABLES_linux_Firewall_Administration (1).pdf
IPTABLES_linux_Firewall_Administration (1).pdf
 
Shell programming
Shell programmingShell programming
Shell programming
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
 
Netcat - A Swiss Army Tool
Netcat - A Swiss Army ToolNetcat - A Swiss Army Tool
Netcat - A Swiss Army Tool
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
Fluentd introduction at ipros
Fluentd introduction at iprosFluentd introduction at ipros
Fluentd introduction at ipros
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scripting
 
Tcpdump
TcpdumpTcpdump
Tcpdump
 
Netcat 101 by-mahesh-beema
Netcat 101 by-mahesh-beemaNetcat 101 by-mahesh-beema
Netcat 101 by-mahesh-beema
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromq
 
Linux Knowledge Transfer
Linux Knowledge TransferLinux Knowledge Transfer
Linux Knowledge Transfer
 
Kernel bug hunting
Kernel bug huntingKernel bug hunting
Kernel bug hunting
 
Basic linux commands
Basic linux commands Basic linux commands
Basic linux commands
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell Scripting
 
Leveraging zeromq for node.js
Leveraging zeromq for node.jsLeveraging zeromq for node.js
Leveraging zeromq for node.js
 

Destacado

Building Complex Topology using NS3
Building Complex Topology using NS3Building Complex Topology using NS3
Building Complex Topology using NS3Rahul Hada
 
Building Topology in NS3
Building Topology in NS3Building Topology in NS3
Building Topology in NS3Rahul Hada
 
1 session installation
1 session installation1 session installation
1 session installationRahul Hada
 
Ns3 implementation wifi
Ns3 implementation wifiNs3 implementation wifi
Ns3 implementation wifiSalah Amean
 
Socio-technical System
Socio-technical SystemSocio-technical System
Socio-technical SystemRahul Hada
 
Tutorial ns 3-tutorial-slides
Tutorial ns 3-tutorial-slidesTutorial ns 3-tutorial-slides
Tutorial ns 3-tutorial-slidesVinayagam D
 
Software Engineering Introduction
Software Engineering IntroductionSoftware Engineering Introduction
Software Engineering IntroductionRahul Hada
 
Support formobility
Support formobilitySupport formobility
Support formobilityRahul Hada
 
Introduction of Cloud Computing
Introduction of Cloud ComputingIntroduction of Cloud Computing
Introduction of Cloud ComputingRahul Hada
 
Mobile transportlayer
Mobile transportlayerMobile transportlayer
Mobile transportlayerRahul Hada
 
Quality planning
Quality planningQuality planning
Quality planningRahul Hada
 
Introduction to Virtualization
Introduction to VirtualizationIntroduction to Virtualization
Introduction to VirtualizationRahul Hada
 
WLAN - IEEE 802.11
WLAN - IEEE 802.11WLAN - IEEE 802.11
WLAN - IEEE 802.11Rahul Hada
 
ns-3: History and Future
ns-3: History and Futurens-3: History and Future
ns-3: History and Futuremathieu_lacage
 
Network Simulation NS3
Network Simulation NS3Network Simulation NS3
Network Simulation NS3baddi youssef
 

Destacado (20)

Building Complex Topology using NS3
Building Complex Topology using NS3Building Complex Topology using NS3
Building Complex Topology using NS3
 
NS3 Overview
NS3 OverviewNS3 Overview
NS3 Overview
 
Building Topology in NS3
Building Topology in NS3Building Topology in NS3
Building Topology in NS3
 
1 session installation
1 session installation1 session installation
1 session installation
 
Ns3 implementation wifi
Ns3 implementation wifiNs3 implementation wifi
Ns3 implementation wifi
 
ns-3 Tutorial
ns-3 Tutorialns-3 Tutorial
ns-3 Tutorial
 
Inheritance
InheritanceInheritance
Inheritance
 
Socio-technical System
Socio-technical SystemSocio-technical System
Socio-technical System
 
Tutorial ns 3-tutorial-slides
Tutorial ns 3-tutorial-slidesTutorial ns 3-tutorial-slides
Tutorial ns 3-tutorial-slides
 
Software Engineering Introduction
Software Engineering IntroductionSoftware Engineering Introduction
Software Engineering Introduction
 
Support formobility
Support formobilitySupport formobility
Support formobility
 
Risk
RiskRisk
Risk
 
Risk
RiskRisk
Risk
 
Introduction of Cloud Computing
Introduction of Cloud ComputingIntroduction of Cloud Computing
Introduction of Cloud Computing
 
Mobile transportlayer
Mobile transportlayerMobile transportlayer
Mobile transportlayer
 
Quality planning
Quality planningQuality planning
Quality planning
 
Introduction to Virtualization
Introduction to VirtualizationIntroduction to Virtualization
Introduction to Virtualization
 
WLAN - IEEE 802.11
WLAN - IEEE 802.11WLAN - IEEE 802.11
WLAN - IEEE 802.11
 
ns-3: History and Future
ns-3: History and Futurens-3: History and Future
ns-3: History and Future
 
Network Simulation NS3
Network Simulation NS3Network Simulation NS3
Network Simulation NS3
 

Similar a Fundamental of Shell Programming

Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them Allegypt
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfHIMANKMISHRA2
 
Bash shell
Bash shellBash shell
Bash shellxylas121
 
101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commandsAcácio Oliveira
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansibleOmid Vahdaty
 
RHCSA EX200 - Summary
RHCSA EX200 - SummaryRHCSA EX200 - Summary
RHCSA EX200 - SummaryNugroho Gito
 
NYPHP March 2009 Presentation
NYPHP March 2009 PresentationNYPHP March 2009 Presentation
NYPHP March 2009 Presentationbrian_dailey
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboyKenneth Geisshirt
 
Bioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekingeBioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekingeProf. Wim Van Criekinge
 
Linux basic for CADD biologist
Linux basic for CADD biologistLinux basic for CADD biologist
Linux basic for CADD biologistAjay Murali
 

Similar a Fundamental of Shell Programming (20)

Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Shellprogramming
ShellprogrammingShellprogramming
Shellprogramming
 
One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them All
 
Licão 05 scripts exemple
Licão 05 scripts exempleLicão 05 scripts exemple
Licão 05 scripts exemple
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdf
 
Bash shell
Bash shellBash shell
Bash shell
 
tools
toolstools
tools
 
tools
toolstools
tools
 
Intro to-puppet
Intro to-puppetIntro to-puppet
Intro to-puppet
 
101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commands
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
RHCSA EX200 - Summary
RHCSA EX200 - SummaryRHCSA EX200 - Summary
RHCSA EX200 - Summary
 
NYPHP March 2009 Presentation
NYPHP March 2009 PresentationNYPHP March 2009 Presentation
NYPHP March 2009 Presentation
 
Unix tips and tricks
Unix tips and tricksUnix tips and tricks
Unix tips and tricks
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
Unix
UnixUnix
Unix
 
Bioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekingeBioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekinge
 
Linux basic for CADD biologist
Linux basic for CADD biologistLinux basic for CADD biologist
Linux basic for CADD biologist
 

Último

UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 

Último (20)

UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
20150722 - AGV
20150722 - AGV20150722 - AGV
20150722 - AGV
 

Fundamental of Shell Programming

  • 1. Fundamental of Linux & Shell Programming Presented By:-Rahul Hada hada.rahul@gmail.com Mob:- 9001806370
  • 2. RoadMap ● Basic of shell programming – – Using variables – Pipes – Performing math – ● Creation of shell program Handling user input Control Structure – – if-then-else Loops ● ● For , while , until Creating Function 18/12/13 OpenLab 2
  • 3. Basic of Shell Programming ● Creating a shell program – Specify the shell you are using in the first line – #!/bin/bash , #!/bin/ksh etc. – Ex: Shell program to print something #!/bin/bash echo “Welcome to the world of Shell Programming” ● Execution of program – Shell script never compile – First change mode then execute – cs@poornima$ chmod 775 hello.sh or chmod u+x – cs@poornima$ ./hello.sh 18/12/13 OpenLab 3
  • 4. Basic of Shell Programming ● Variables – we can access the value of variable using $ sign. – Types of variables ● ● ● 18/12/13 Environment Variables – stores specific system information. ex. $HOME,$SHELL,$LANGUAGE, $HOSTNAME,$HOSTTYPE etc User Variables – stores our own values in a variable. Ex: #!/bin/bash | #!/bin/bash a=5 | read a echo “Value of a=$a” | echo “Value of a=$a” OpenLab 4
  • 5. Basic of Shell Programming ● Backtick (` `) – It allow you to assign the output of a shell command to a variable – Ex:#!/bin/bash 18/12/13 | #!/bin/bash | d=`date` | echo date | echo $d date | #!/bin/bash | OpenLab 5
  • 6. Basic of Shell Programming ● Pipes (|) – The output of one command is the input of the other command. – Command1 | command2 – grep poornima history.txt | wc -l – Ex: #!/bin/bash a=`grep poornima history | wc -l` echo “Number of Lines Containing Pattern=$a” 18/12/13 OpenLab 6
  • 7. Basic of Shell Programming ● Evaluating expression – Two ways to evaluate ● ● ● Using expr expression Using $[ expression] They work differently for multiplication ( * ) | #!/bin/bash | b=5 c=6 | c=6 a=`expr $b + $c` | a=$[$b +$c] echo “Sum =$a” 18/12/13 #!/bin/bash b=5 – | echo “Sum=$a” OpenLab 7
  • 8. Basic of Shell Programming ● Exiting the script Every command runs in the shell uses an exit status. ● Whose value vary from 0-255 ● Value stores on ? Or $? ● Explicit exit status from script ● cs@poornima$ echo $? #!/bin/bash #!/bin/bash | #!/bin/bash hello | ls -ytr | ls -l echo $? 18/12/13 | | echo $? | echo $? OpenLab 8
  • 9. Basic of Shell Programming ● Exit Status Codes – 0 -------- successful completion of the command – 2 -------- misuse of shell commands – 127 -------- command not found – 130 -------- command terminated with Clt-C example using program 18/12/13 OpenLab 9
  • 10. Basic of Shell Programming ● Command line Parameters – It allow you to add data values to the command line when you execute the script. – Ex : cs@poornima$./sum.sh 23 56 – Shell uses special variable, called positional parameters. – Represented from 0 – 9 i.e $0 - $9 #!/bin/bash s=`expr $1 + $2` echo “Sum=$s” 18/12/13 OpenLab 10
  • 11. Basic of Shell Programming ● Some special variables releated to the command line parameters – $* , $@ , $# #!/bin/bash #!/bin/bash | #!/bin/bash echo $# – | | echo $* | echo $@ Shift command – it downgrades each parameter variable one position by default. #!/bin/bash ./cli1.sh 3 4 5 shift shift 18/12/13 echo $1 OpenLab 11
  • 12. Control Structure ● If-then-else testing of if condition using test & [ ] Structure Example if command | #!/bin/bash then | if test $1 -gt $2 command fi | then | echo “First CL is greater” | else | echo “Second CL is greater” 18/12/13 OpenLab 12
  • 13. Control Structure ● Test of numeric comparisons – n1 -gt n2 – n1 -eq n2 -- check if n1 is equal to n2 – n1 -lt n2 -- check if n1 is lesser then n2 – n1 -le n2 -- check if n1 is lesser or equal to n2 – n1 -ge n2 -- check if n1 is greater or equal to n2 – n1 -ne n2 -- check if n1 is not equal to n2 18/12/13 -- check if n1 is greater then n2 OpenLab 13
  • 14. Control Structure ● Test of string comparisons – str1 = srt2 -- check if str1 is the same as str2 – str1 != str2 – check if str1 is not equal to str2 – str1 > str2 – check if str1 is greater then str2 – str1 < str2 – check is str1 is lesser then str2 – -n str1--check if str1 has a length greater then zero – -z str1-- check if str1 has length is zero Note : Must use escape () symbol while using > or < 18/12/13 OpenLab 14
  • 15. Control Structure ● Test file comparisons – -d file -- check if file exist and is a directory – -e file -- check if file exist – -f file -- check if file exist and is a file – -r file -- check if file exist and is readable – -w file – check if file exist and is writable – -x file – check if file exist and is executable – and, few more 18/12/13 OpenLab 15
  • 16. Control Structure ● Case Statement case variable in | | pattern3) command ;; case $ch in | command;; read ch | pattern1 | pattern2) #!/bin/bash a|e|i|o|u) | | *) command;; echo “char. is vowel” ;; | esac *) echo ”char is not vowel” | 18/12/13 | ;; | esac OpenLab 16
  • 17. Control Structure ● Loops types in shell for , while , until for var in list do command done ● Different ways to represent for loop ● Can redirect the output of for loop in file ● For loop can take input from file 18/12/13 OpenLab 17
  • 18. Control Structure ● While Loop while test condition do commands done 18/12/13 OpenLab 18
  • 19. Control Structure ● Untile loop – works opposite way of while loop until test commands do commands done 18/12/13 OpenLab 19
  • 20. Guess output ? #!/bin/bash | #!/bin/bash for var in” $*” | for var in “$@” do | do echo “Output=$var” | echo “Output=$var” done 18/12/13 |done OpenLab 20
  • 21. Creation of function ● Function – reuse of same shell code ● In shell function is a mini-script ● Creation of function by two ways function name { commands } | name () { | commands | } | 18/12/13 OpenLab 21
  • 22. Creation of Function ● ● Passing parameters in function Returing values from function can be three types :– By Default it return exit status of the last executed command in the function – We can also use return to modify the exit status as per our own requirement – Using echo to return values 18/12/13 OpenLab 22
  • 23. Creation of Function ● Passing array in function ● Decleration of array Ex:- myarray = (1 2 3 4 5) access values of array -- ${myarray[$1]} index 1 ${myarray[*]} all array – 18/12/13 Example FunArrVar.sh OpenLab 23