SlideShare a Scribd company logo
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

More Related Content

PDF
Socket Programming using Java
PPTX
The TCP/IP Stack in the Linux Kernel
PDF
Experimental dtrace
PDF
Painless Perl Ports with cpan2port
PDF
JS introduction
PDF
Syslog Protocols
PDF
Monitoring with Syslog and EventMachine
PDF
NS3 Tech Talk
Socket Programming using Java
The TCP/IP Stack in the Linux Kernel
Experimental dtrace
Painless Perl Ports with cpan2port
JS introduction
Syslog Protocols
Monitoring with Syslog and EventMachine
NS3 Tech Talk

What's hot (20)

PDF
introduction to linux kernel tcp/ip ptocotol stack
PDF
Complete Guide for Linux shell programming
PPTX
PPT
Shell Scripting in Linux
PDF
Unix Shell Script
PDF
IPTABLES_linux_Firewall_Administration (1).pdf
PPT
Shell programming
ODP
Introduction to Shell script
PPTX
Netcat - A Swiss Army Tool
PDF
Embedded linux network device driver development
PDF
Fluentd introduction at ipros
PPTX
Bash shell scripting
PDF
Tcpdump
PDF
Netcat 101 by-mahesh-beema
KEY
Distributed app development with nodejs and zeromq
ODP
Linux Knowledge Transfer
PDF
Kernel bug hunting
PDF
Basic linux commands
PDF
Unix Shell Scripting
PPT
Leveraging zeromq for node.js
introduction to linux kernel tcp/ip ptocotol stack
Complete Guide for Linux shell programming
Shell Scripting in Linux
Unix Shell Script
IPTABLES_linux_Firewall_Administration (1).pdf
Shell programming
Introduction to Shell script
Netcat - A Swiss Army Tool
Embedded linux network device driver development
Fluentd introduction at ipros
Bash shell scripting
Tcpdump
Netcat 101 by-mahesh-beema
Distributed app development with nodejs and zeromq
Linux Knowledge Transfer
Kernel bug hunting
Basic linux commands
Unix Shell Scripting
Leveraging zeromq for node.js
Ad

Viewers also liked (20)

PDF
Building Complex Topology using NS3
PDF
NS3 Overview
PDF
Building Topology in NS3
PDF
1 session installation
PDF
Ns3 implementation wifi
PDF
ns-3 Tutorial
PDF
Inheritance
PDF
Socio-technical System
PDF
Tutorial ns 3-tutorial-slides
PDF
Software Engineering Introduction
PDF
Support formobility
PPT
PPT
PDF
Introduction of Cloud Computing
PDF
Mobile transportlayer
PPT
Quality planning
ODP
Introduction to Virtualization
PDF
WLAN - IEEE 802.11
PDF
ns-3: History and Future
PDF
Network Simulation NS3
Building Complex Topology using NS3
NS3 Overview
Building Topology in NS3
1 session installation
Ns3 implementation wifi
ns-3 Tutorial
Inheritance
Socio-technical System
Tutorial ns 3-tutorial-slides
Software Engineering Introduction
Support formobility
Introduction of Cloud Computing
Mobile transportlayer
Quality planning
Introduction to Virtualization
WLAN - IEEE 802.11
ns-3: History and Future
Network Simulation NS3
Ad

Similar to Fundamental of Shell Programming (20)

PDF
Module 03 Programming on Linux
PPT
2-introduction_to_shell_scripting
PPT
34-shell-programming asda asda asd asd.ppt
PPTX
Basics of shell programming
PPTX
Basics of shell programming
PPTX
Shell & Shell Script
PPTX
Shell & Shell Script
PDF
Lab4 scripts
PPT
Operating system (remuel)
DOCX
What is a shell script
PPT
34-shell-programming.ppt
PPTX
Unix shell scripting basics
PPT
Chap06
PDF
Linux shell script-1
PPT
390aLecture05_12sp.ppt
PPTX
Shell Script Tutorial
PPTX
Unix - Shell Scripts
DOCX
Quize on scripting shell
PPTX
Shell scripting
Module 03 Programming on Linux
2-introduction_to_shell_scripting
34-shell-programming asda asda asd asd.ppt
Basics of shell programming
Basics of shell programming
Shell & Shell Script
Shell & Shell Script
Lab4 scripts
Operating system (remuel)
What is a shell script
34-shell-programming.ppt
Unix shell scripting basics
Chap06
Linux shell script-1
390aLecture05_12sp.ppt
Shell Script Tutorial
Unix - Shell Scripts
Quize on scripting shell
Shell scripting

Recently uploaded (20)

PDF
Modernizing your data center with Dell and AMD
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
A Presentation on Artificial Intelligence
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPT
Teaching material agriculture food technology
Modernizing your data center with Dell and AMD
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Big Data Technologies - Introduction.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Machine learning based COVID-19 study performance prediction
A Presentation on Artificial Intelligence
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Teaching material agriculture food technology

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