SlideShare a Scribd company logo
SHELL PROGRAMMING
@2020 copyright KalKey training
SHELL SCRIPTS (1)
 Basically, a shell script is a text file with Unix
commands in it.
 Shell scripts usually begin with a #! and a shell name
– For example: #!/bin/sh
– If they do not, the user's current shell will be used
 Any Unix command can go in a shell script
– Commands are executed in order or in the flow
determined by control statements.
 Different shells have different control structures
– The #! line is very important
– We will write shell scripts with the Bourne shell (sh)
@2020 copyright KalKey training
SHELL SCRIPTS (2)
Why write shell scripts?
– To avoid repetition:
If you do a sequence of steps with standard
Unix commands over and over, why not do it all
with just one command?
– To automate difficult tasks:
Many commands have subtle and difficult
options that you don’t want to figure out or
remember every time.
@2020 copyright KalKey training
A SIMPLE EXAMPLE (1)
 tr abcdefghijklmnopqrstuvwxyz 
thequickbrownfxjmpsvalzydg < file1 > file2
– “encrypts” file1 into file2
 Record this command into shell script files:
– myencrypt
#!/bin/sh
tr abcdefghijklmnopqrstuvwxyz 
thequickbrownfxjmpsvalzydg
– mydecrypt
#!/bin/sh
tr thequickbrownfxjmpsvalzydg 
abcdefghijklmnopqrstuvwxyz @2020 copyright KalKey training
BOURNE SHELL VARIABLES
Remember: Bourne shell variables are different
from variables in csh and tcsh!
– Examples in sh:
PATH=$PATH:$HOME/bin
HA=$1
PHRASE="House on the hill"
export PHRASE
Note: no space
around =
Make PHRASE an
environment variable
@2020 copyright KalKey training
ASSIGNING COMMAND OUTPUT TO A
VARIABLE
Using backquotes, we can assign the output of
a command to a variable:
#!/bin/sh
files=`ls`
echo $files
Very useful in numerical computation:
#!/bin/sh
value=`expr 12345 + 54321`
echo $value
@2020 copyright KalKey training
USING EXPR FOR CALCULATIONS
 Variables as arguments:
% count=5
% count=`expr $count + 1`
% echo $count
6
– Variables are replaced with their values by the shell!
 expr supports the following operators:
– arithmetic operators: +,-,*,/,%
– comparison operators: <, <=, ==, !=, >=, >
– boolean/logical operators: &, |
– parentheses: (, )
– precedence is the same as C, Java
@2020 copyright KalKey training
CONTROL STATEMENTS
Without control statements, execution within
a shell scripts flows from one statement to the
next in succession.
Control statements control the flow of
execution in a programming language
The three most common types of control
statements:
– conditionals: if/then/else, case, ...
– loop statements: while, for, until, do, ...
– branch statements: subroutine calls (good), goto
(bad)
@2020 copyright KalKey training
FOR LOOPS
for loops allow the repetition of a command
for a specific set of values
Syntax:
for var in value1 value2 ...
do
command_set
done
– command_set is executed with each value of var
(value1, value2, ...) in sequence
@2020 copyright KalKey training
FOR LOOP EXAMPLE (1)
#!/bin/sh
# timestable – print out a multiplication table
for i in 1 2 3
do
for j in 1 2 3
do
value=`expr $i * $j`
echo -n "$value "
done
echo
done
@2020 copyright KalKey training
FOR LOOP EXAMPLE (2)
#!/bin/sh
# file-poke – tell us stuff about files
files=`ls`
for i in $files
do
echo -n "$i "
grep $i $i
done
– Find filenames in files in current directory
@2020 copyright KalKey training
FOR LOOP EXAMPLE (3)
#!/bin/sh
# file-poke – tell us stuff about files
for i in *; do
echo -n "$i "
grep $i $i
done
– Same as previous slide, only a little more
condensed.
@2020 copyright KalKey training
CONDITIONALS
 Conditionals are used to “test” something.
– In Java or C, they test whether a Boolean variable is true
or false.
– In a Bourne shell script, the only thing you can test is
whether or not a command is “successful”
 Every well behaved command returns back a return
code.
– 0 if it was successful
– Non-zero if it was unsuccessful (actually 1..255)
– We will see later that this is different from true/false
conditions in C.
@2020 copyright KalKey training
THE IF STATEMENT
 Simple form:
if decision_command_1
then
command_set_1
fi
 Example:
if grep unix myfile >/dev/null
then
echo "It's there"
fi
grep returns 0 if it finds something
returns non-zero otherwise
redirect to /dev/null so that
"intermediate" results do not get
printed
@2020 copyright KalKey training
IF AND ELSE
if grep "UNIX" myfile >/dev/null
then
echo UNIX occurs in myfile
else
echo No!
echo UNIX does not occur in myfile
fi
@2020 copyright KalKey training
IF AND ELIF
if grep "UNIX" myfile >/dev/null
then
echo "UNIX occurs in file"
elif grep "DOS" myfile >/dev/null
then
echo "Unix does not occur, but DOS does"
else
echo "Nobody is there"
fi
@2020 copyright KalKey training

More Related Content

PPTX
Shell scripting
ODP
Shellscripting
PPTX
Basics of shell programming
PPT
Chap06
PPTX
Linux shell env
PDF
Quick start bash script
PPTX
Unix shell scripts
PPT
Shell programming
Shell scripting
Shellscripting
Basics of shell programming
Chap06
Linux shell env
Quick start bash script
Unix shell scripts
Shell programming

What's hot (20)

PDF
Shell scripting
PPTX
Unix - Shell Scripts
ODP
PPTX
SHELL PROGRAMMING
PPTX
system management -shell programming by gaurav raikar
PPTX
Shell & Shell Script
PPT
Shell Scripting
PPT
Bash shell
PDF
Shell scripting
PPTX
Easiest way to start with Shell scripting
PPTX
Bash Shell Scripting
PPT
Linux shell scripting
PPTX
Bash Shell Scripting
PDF
Complete Guide for Linux shell programming
PDF
BASH Guide Summary
PDF
Unix shell scripting tutorial
PPT
Unix And Shell Scripting
PPTX
Bash shell scripting
PPT
Unix Shell Scripting Basics
PDF
Shell script-sec
Shell scripting
Unix - Shell Scripts
SHELL PROGRAMMING
system management -shell programming by gaurav raikar
Shell & Shell Script
Shell Scripting
Bash shell
Shell scripting
Easiest way to start with Shell scripting
Bash Shell Scripting
Linux shell scripting
Bash Shell Scripting
Complete Guide for Linux shell programming
BASH Guide Summary
Unix shell scripting tutorial
Unix And Shell Scripting
Bash shell scripting
Unix Shell Scripting Basics
Shell script-sec
Ad

Similar to Shell programming 1.ppt (20)

PPTX
Shell programming 2
PPTX
Shell programming 2
PPT
34-shell-programming.ppt
PPT
34-shell-programming asda asda asd asd.ppt
PPT
ShellProgramming and Script in operating system
PPTX
Unix shell scripting basics
PPT
Spsl by sasidhar 3 unit
PPT
Unix Shell Scripting Basics
DOCX
What is a shell script
PPTX
Shell & Shell Script
PPT
Introduction to shell scripting ____.ppt
PPTX
Shell Script Tutorial
PPT
Unix shell scripting basics
PPTX
Shell Programming Language in Operating System .pptx
PPT
PDF
Shell Script Linux
PDF
Operating_System_Lab_ClassOperating_System_2.pdf
PPTX
Basics of shell programming
PPT
Bash Programming
PDF
Unixshellscript 100406085942-phpapp02
Shell programming 2
Shell programming 2
34-shell-programming.ppt
34-shell-programming asda asda asd asd.ppt
ShellProgramming and Script in operating system
Unix shell scripting basics
Spsl by sasidhar 3 unit
Unix Shell Scripting Basics
What is a shell script
Shell & Shell Script
Introduction to shell scripting ____.ppt
Shell Script Tutorial
Unix shell scripting basics
Shell Programming Language in Operating System .pptx
Shell Script Linux
Operating_System_Lab_ClassOperating_System_2.pdf
Basics of shell programming
Bash Programming
Unixshellscript 100406085942-phpapp02
Ad

More from Kalkey (20)

PPT
Docker swarm
PPTX
Docker advance topic (2)
PPTX
Docker introduction (1)
PPTX
Nexus
PPTX
Sonarqube
PPTX
Introduction of tomcat
PPTX
Jenkins advance topic
PPTX
Jenkins introduction
PPTX
Intro
PPTX
Terraform day 3
PPTX
Terraform day 2
PPTX
Terraform day 1
PPTX
Ansible day 3
PPTX
Cloud computing 1
PPTX
Adnible day 2.ppt
PPTX
Debasihish da final.ppt
PPTX
Linux day 3ppt
PPTX
Ansible day 1.ppt
PPTX
Linux day 2.ppt
PPTX
Docker advance topic
Docker swarm
Docker advance topic (2)
Docker introduction (1)
Nexus
Sonarqube
Introduction of tomcat
Jenkins advance topic
Jenkins introduction
Intro
Terraform day 3
Terraform day 2
Terraform day 1
Ansible day 3
Cloud computing 1
Adnible day 2.ppt
Debasihish da final.ppt
Linux day 3ppt
Ansible day 1.ppt
Linux day 2.ppt
Docker advance topic

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Pre independence Education in Inndia.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
master seminar digital applications in india
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Lesson notes of climatology university.
Microbial diseases, their pathogenesis and prophylaxis
Microbial disease of the cardiovascular and lymphatic systems
Pre independence Education in Inndia.pdf
Insiders guide to clinical Medicine.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
O5-L3 Freight Transport Ops (International) V1.pdf
Final Presentation General Medicine 03-08-2024.pptx
Complications of Minimal Access Surgery at WLH
O7-L3 Supply Chain Operations - ICLT Program
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
master seminar digital applications in india
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Module 4: Burden of Disease Tutorial Slides S2 2025
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Lesson notes of climatology university.

Shell programming 1.ppt

  • 2. SHELL SCRIPTS (1)  Basically, a shell script is a text file with Unix commands in it.  Shell scripts usually begin with a #! and a shell name – For example: #!/bin/sh – If they do not, the user's current shell will be used  Any Unix command can go in a shell script – Commands are executed in order or in the flow determined by control statements.  Different shells have different control structures – The #! line is very important – We will write shell scripts with the Bourne shell (sh) @2020 copyright KalKey training
  • 3. SHELL SCRIPTS (2) Why write shell scripts? – To avoid repetition: If you do a sequence of steps with standard Unix commands over and over, why not do it all with just one command? – To automate difficult tasks: Many commands have subtle and difficult options that you don’t want to figure out or remember every time. @2020 copyright KalKey training
  • 4. A SIMPLE EXAMPLE (1)  tr abcdefghijklmnopqrstuvwxyz thequickbrownfxjmpsvalzydg < file1 > file2 – “encrypts” file1 into file2  Record this command into shell script files: – myencrypt #!/bin/sh tr abcdefghijklmnopqrstuvwxyz thequickbrownfxjmpsvalzydg – mydecrypt #!/bin/sh tr thequickbrownfxjmpsvalzydg abcdefghijklmnopqrstuvwxyz @2020 copyright KalKey training
  • 5. BOURNE SHELL VARIABLES Remember: Bourne shell variables are different from variables in csh and tcsh! – Examples in sh: PATH=$PATH:$HOME/bin HA=$1 PHRASE="House on the hill" export PHRASE Note: no space around = Make PHRASE an environment variable @2020 copyright KalKey training
  • 6. ASSIGNING COMMAND OUTPUT TO A VARIABLE Using backquotes, we can assign the output of a command to a variable: #!/bin/sh files=`ls` echo $files Very useful in numerical computation: #!/bin/sh value=`expr 12345 + 54321` echo $value @2020 copyright KalKey training
  • 7. USING EXPR FOR CALCULATIONS  Variables as arguments: % count=5 % count=`expr $count + 1` % echo $count 6 – Variables are replaced with their values by the shell!  expr supports the following operators: – arithmetic operators: +,-,*,/,% – comparison operators: <, <=, ==, !=, >=, > – boolean/logical operators: &, | – parentheses: (, ) – precedence is the same as C, Java @2020 copyright KalKey training
  • 8. CONTROL STATEMENTS Without control statements, execution within a shell scripts flows from one statement to the next in succession. Control statements control the flow of execution in a programming language The three most common types of control statements: – conditionals: if/then/else, case, ... – loop statements: while, for, until, do, ... – branch statements: subroutine calls (good), goto (bad) @2020 copyright KalKey training
  • 9. FOR LOOPS for loops allow the repetition of a command for a specific set of values Syntax: for var in value1 value2 ... do command_set done – command_set is executed with each value of var (value1, value2, ...) in sequence @2020 copyright KalKey training
  • 10. FOR LOOP EXAMPLE (1) #!/bin/sh # timestable – print out a multiplication table for i in 1 2 3 do for j in 1 2 3 do value=`expr $i * $j` echo -n "$value " done echo done @2020 copyright KalKey training
  • 11. FOR LOOP EXAMPLE (2) #!/bin/sh # file-poke – tell us stuff about files files=`ls` for i in $files do echo -n "$i " grep $i $i done – Find filenames in files in current directory @2020 copyright KalKey training
  • 12. FOR LOOP EXAMPLE (3) #!/bin/sh # file-poke – tell us stuff about files for i in *; do echo -n "$i " grep $i $i done – Same as previous slide, only a little more condensed. @2020 copyright KalKey training
  • 13. CONDITIONALS  Conditionals are used to “test” something. – In Java or C, they test whether a Boolean variable is true or false. – In a Bourne shell script, the only thing you can test is whether or not a command is “successful”  Every well behaved command returns back a return code. – 0 if it was successful – Non-zero if it was unsuccessful (actually 1..255) – We will see later that this is different from true/false conditions in C. @2020 copyright KalKey training
  • 14. THE IF STATEMENT  Simple form: if decision_command_1 then command_set_1 fi  Example: if grep unix myfile >/dev/null then echo "It's there" fi grep returns 0 if it finds something returns non-zero otherwise redirect to /dev/null so that "intermediate" results do not get printed @2020 copyright KalKey training
  • 15. IF AND ELSE if grep "UNIX" myfile >/dev/null then echo UNIX occurs in myfile else echo No! echo UNIX does not occur in myfile fi @2020 copyright KalKey training
  • 16. IF AND ELIF if grep "UNIX" myfile >/dev/null then echo "UNIX occurs in file" elif grep "DOS" myfile >/dev/null then echo "Unix does not occur, but DOS does" else echo "Nobody is there" fi @2020 copyright KalKey training