SlideShare a Scribd company logo
SHELL PROGRAMMING
2
@2020 copyright KalKey training
USE OF SEMICOLONS
Instead of being on separate lines, statements
can be separated by a semicolon (;)
– For example:
if grep "UNIX" myfile; then echo "Got it"; fi
– This actually works anywhere in the shell.
% cwd=`pwd`; cd $HOME; ls; cd $cwd
@2020 copyright KalKey training
USE OF COLON
 Sometimes it is useful to have a command which
does “nothing”.
 The : (colon) command in Unix does nothing
#!/bin/sh
if grep unix myfile
then
:
else
echo "Sorry, unix was not found"
fi
@2020 copyright KalKey training
THE TEST COMMAND – STRING TESTS
 test –z string is string of length 0?
 test string1 = string2 does string1 equal string2?
 test string1 != string2 not equal?
 Example:
if test -z $REMOTEHOST
then
:
else
DISPLAY="$REMOTEHOST:0"
export DISPLAY
fi
@2020 copyright KalKey training
THE TEST COMMAND – INTEGER TESTS
Integers can also be compared:
– Use -eq, -ne, -lt, -le, -gt, -ge
For example:
#!/bin/sh
smallest=10000
for i in 5 8 19 8 7 3; do
if test $i -lt $smallest; then
smallest=$i
fi
done
echo $smallest
@2020 copyright KalKey training
USE OF [ ]
The test program has an alias as [ ]
– Each bracket must be surrounded by spaces!
– This is supposed to be a bit easier to read.
For example:
#!/bin/sh
smallest=10000
for i in 5 8 19 8 7 3; do
if [ $i -lt $smallest ] ; then
smallest=$i
fi
done
echo $smallest
@2020 copyright KalKey training
THE WHILE LOOP
 While loops repeat statements as long as the
next Unix command is successful.
 For example:
#!/bin/sh
i=1
sum=0
while [ $i -le 100 ]; do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo The sum is $sum.
@2020 copyright KalKey training
THE UNTIL LOOP
 Until loops repeat statements until the next
Unix command is successful.
 For example:
#!/bin/sh
x=1
until [ $x -gt 3 ]; do
echo x = $x
x=`expr $x + 1`
done
@2020 copyright KalKey training
COMMAND LINE ARGUMENTS (1)
 Shell scripts would not be very useful if we could not
pass arguments to them on the command line
 Shell script arguments are “numbered” from left to
right
– $1 - first argument after command
– $2 - second argument after command
– ... up to $9
– They are called “positional parameters”.
@2020 copyright KalKey training
COMMAND LINE ARGUMENTS (2)
Example: get a particular line of a file
– Write a command with the format:
getlineno linenumber filename
#!/bin/sh
head -$1 $2 | tail -1
Other variables related to arguments:
$0 name of the command running
$* All the arguments (even if there are more than
9)
$# the number of arguments
@2020 copyright KalKey training
COMMAND LINE ARGUMENTS (3)
 Example: print the oldest files in a directory
#! /bin/sh
# oldest -- examine the oldest parts of a directory
HOWMANY=$1
shift
ls -lt $* | tail +2 | tail $HOWMANY
 The shift command shifts all the arguments to the left
– $1 = $2, $2 =$3, $3 = $4, ...
– $1 is lost (but we have saved it in $HOWMANY)
– The value of $# is changed ($# - 1)
– useful when there are more than 9 arguments
 The “tail +2” command removes the first line.
@2020 copyright KalKey training
MORE ON BOURNE SHELL VARIABLES (1)
There are three basic types of variables in a
shell script:
– Positional variables ...
$1, $2, $3, ..., $9
– Keyword variables ...
Like $PATH, $HOWMANY, and anything else we
may define.
– Special variables ...
@2020 copyright KalKey training
MORE ON BOURNE SHELL VARIABLES (2)
Special variables:
– $*, $# -- all the arguments, the number of
the arguments
– $$ -- the process id of the current shell
– $? -- return value of last foreground
process to finish
-- more on this one later
– There are others you can find out about with man
sh
@2020 copyright KalKey training
READING VARIABLES FROM STANDARD INPUT
(1)
 The read command reads one line of input from
the terminal and assigns it to variables give as
arguments
 Syntax: read var1 var2 var3 ...
Action: reads a line of input from standard input
Assign first word to var1, second word to var2, ...
The last variable gets any excess words on the line.
@2020 copyright KalKey training
READING VARIABLES FROM STANDARD INPUT
(2)
 Example:
% read X Y Z
Here are some words as input
% echo $X
Here
% echo $Y
are
% echo $Z
some words as input
@2020 copyright KalKey training
THE CASE STATEMENT
 The case statement supports multiway branching based
on the value of a single string.
 General form:
case string in
pattern1)
command_set_11
;;
pattern2)
command_set_2
;;
…
esac
@2020 copyright KalKey training
CASE EXAMPLE
#!/bin/sh
echo -n 'Choose command [1-4] > '
read reply
echo
case $reply in
"1")
date
;;
"2"|"3")
pwd
;;
"4")
ls
;;
*)
echo Illegal choice!
;;
esac
Use the pipe symbol “|” as a logical
or between several choices.
Provide a default case when no
other cases are matched.
@2020 copyright KalKey training
REDIRECTION IN BOURNE SHELL SCRIPTS (1)
 Standard input is redirected the same (<).
 Standard output can be redirected the same (>).
– Can also be directed using the notation 1>
– For example: cat x 1> ls.txt (only stdout)
 Standard error is redirected using the notation 2>
– For example: cat x y 1> stdout.txt 2> stderr.txt
 Standard output and standard error can be redirected to
the same file using the notation 2>&1
– For example: cat x y > xy.txt 2>&1
 Standard output and standard error can be piped to the
same command using similar notation
– For example: cat x y 2>&1 | grep text
@2020 copyright KalKey training
REDIRECTION IN BOURNE SHELL SCRIPTS (2)
 Shell scripts can also supply standard input to
commands from text embedded in the script itself.
 General form: command << word
– Standard input for command follows this line up to, but not
including, the line beginning with word.
 Example:
#!/bin/sh
grep 'hello' << EOF
This is some sample text.
Here is a line with hello in it.
Here is another line with hello.
No more lines with that word.
EOF
Only these two lines will be
matched and displayed.
@2020 copyright KalKey training

More Related Content

What's hot

Bash shell
Bash shellBash shell
Bash shell
xylas121
 
Unix - Shell Scripts
Unix - Shell ScriptsUnix - Shell Scripts
Unix - Shell Scripts
ananthimurugesan
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
Nihar Ranjan Paital
 
Unit vii wp ppt
Unit vii wp pptUnit vii wp ppt
Unit vii wp ppt
Bhavsingh Maloth
 
Slides
SlidesSlides
Learn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningLearn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square Learning
ASIT Education
 
Ruby programming introduction
Ruby programming introductionRuby programming introduction
Ruby programming introduction
ASIT Education
 
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functions
Docent Education
 
01c shell
01c shell01c shell
01c shell
Kevin Lee
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
Shankar D
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Aaron Zauner
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Hoàng Lâm Huỳnh
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
daoswald
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awk
Yogesh Sawant
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
kayalkarnan
 
Linux com
Linux comLinux com
Namespace--defining same identifiers again
Namespace--defining same identifiers againNamespace--defining same identifiers again
Namespace--defining same identifiers again
Ajay Chimmani
 
Quize on scripting shell
Quize on scripting shellQuize on scripting shell
Quize on scripting shell
lebse123
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
Dr.Ravi
 

What's hot (20)

Bash shell
Bash shellBash shell
Bash shell
 
Unix - Shell Scripts
Unix - Shell ScriptsUnix - Shell Scripts
Unix - Shell Scripts
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
Unit vii wp ppt
Unit vii wp pptUnit vii wp ppt
Unit vii wp ppt
 
Slides
SlidesSlides
Slides
 
Learn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningLearn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square Learning
 
Ruby programming introduction
Ruby programming introductionRuby programming introduction
Ruby programming introduction
 
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functions
 
01c shell
01c shell01c shell
01c shell
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-liners
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awk
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
 
Linux com
Linux comLinux com
Linux com
 
Namespace--defining same identifiers again
Namespace--defining same identifiers againNamespace--defining same identifiers again
Namespace--defining same identifiers again
 
Quize on scripting shell
Quize on scripting shellQuize on scripting shell
Quize on scripting shell
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 

Similar to Shell programming 2

34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
Unix
UnixUnix
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
Gaurav Shinde
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ewout2
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
Chandan Kumar Rana
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
Chandan Kumar Rana
 
Shell programming
Shell programmingShell programming
Shell programming
Moayad Moawiah
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
Mufaddal Haidermota
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
Kuntal Bhowmick
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
Ahmed El-Arabawy
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
jinal thakrar
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
Sasidhar Kothuru
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
Narendra Sisodiya
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
Acácio Oliveira
 
Wildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell VariablesWildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell Variables
Gaurav Bisht
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
Quang Minh Đoàn
 
DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foo
brian_dailey
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1
Leandro Lima
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting language
Vamshi Santhapuri
 

Similar to Shell programming 2 (20)

34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
Unix
UnixUnix
Unix
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Shell programming
Shell programmingShell programming
Shell programming
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Wildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell VariablesWildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell Variables
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
 
DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foo
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting language
 

More from Kalkey

Docker swarm
Docker swarmDocker swarm
Docker swarm
Kalkey
 
Docker advance topic (2)
Docker advance topic (2)Docker advance topic (2)
Docker advance topic (2)
Kalkey
 
Docker introduction (1)
Docker introduction (1)Docker introduction (1)
Docker introduction (1)
Kalkey
 
Nexus
NexusNexus
Nexus
Kalkey
 
Sonarqube
SonarqubeSonarqube
Sonarqube
Kalkey
 
Introduction of tomcat
Introduction of tomcatIntroduction of tomcat
Introduction of tomcat
Kalkey
 
Jenkins advance topic
Jenkins advance topicJenkins advance topic
Jenkins advance topic
Kalkey
 
Jenkins introduction
Jenkins introductionJenkins introduction
Jenkins introduction
Kalkey
 
Intro
IntroIntro
Intro
Kalkey
 
Terraform day 3
Terraform day 3Terraform day 3
Terraform day 3
Kalkey
 
Terraform day 2
Terraform day 2Terraform day 2
Terraform day 2
Kalkey
 
Terraform day 1
Terraform day 1Terraform day 1
Terraform day 1
Kalkey
 
Ansible day 3
Ansible day 3Ansible day 3
Ansible day 3
Kalkey
 
Cloud computing 1
Cloud computing  1Cloud computing  1
Cloud computing 1
Kalkey
 
Adnible day 2.ppt
Adnible day   2.pptAdnible day   2.ppt
Adnible day 2.ppt
Kalkey
 
Debasihish da final.ppt
Debasihish da final.pptDebasihish da final.ppt
Debasihish da final.ppt
Kalkey
 
Linux day 3ppt
Linux day 3pptLinux day 3ppt
Linux day 3ppt
Kalkey
 
Ansible day 1.ppt
Ansible day 1.pptAnsible day 1.ppt
Ansible day 1.ppt
Kalkey
 
Linux day 2.ppt
Linux day  2.pptLinux day  2.ppt
Linux day 2.ppt
Kalkey
 
Docker advance topic
Docker advance topicDocker advance topic
Docker advance topic
Kalkey
 

More from Kalkey (20)

Docker swarm
Docker swarmDocker swarm
Docker swarm
 
Docker advance topic (2)
Docker advance topic (2)Docker advance topic (2)
Docker advance topic (2)
 
Docker introduction (1)
Docker introduction (1)Docker introduction (1)
Docker introduction (1)
 
Nexus
NexusNexus
Nexus
 
Sonarqube
SonarqubeSonarqube
Sonarqube
 
Introduction of tomcat
Introduction of tomcatIntroduction of tomcat
Introduction of tomcat
 
Jenkins advance topic
Jenkins advance topicJenkins advance topic
Jenkins advance topic
 
Jenkins introduction
Jenkins introductionJenkins introduction
Jenkins introduction
 
Intro
IntroIntro
Intro
 
Terraform day 3
Terraform day 3Terraform day 3
Terraform day 3
 
Terraform day 2
Terraform day 2Terraform day 2
Terraform day 2
 
Terraform day 1
Terraform day 1Terraform day 1
Terraform day 1
 
Ansible day 3
Ansible day 3Ansible day 3
Ansible day 3
 
Cloud computing 1
Cloud computing  1Cloud computing  1
Cloud computing 1
 
Adnible day 2.ppt
Adnible day   2.pptAdnible day   2.ppt
Adnible day 2.ppt
 
Debasihish da final.ppt
Debasihish da final.pptDebasihish da final.ppt
Debasihish da final.ppt
 
Linux day 3ppt
Linux day 3pptLinux day 3ppt
Linux day 3ppt
 
Ansible day 1.ppt
Ansible day 1.pptAnsible day 1.ppt
Ansible day 1.ppt
 
Linux day 2.ppt
Linux day  2.pptLinux day  2.ppt
Linux day 2.ppt
 
Docker advance topic
Docker advance topicDocker advance topic
Docker advance topic
 

Recently uploaded

Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Vivekanand Anglo Vedic Academy
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 

Recently uploaded (20)

Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 

Shell programming 2

  • 2. USE OF SEMICOLONS Instead of being on separate lines, statements can be separated by a semicolon (;) – For example: if grep "UNIX" myfile; then echo "Got it"; fi – This actually works anywhere in the shell. % cwd=`pwd`; cd $HOME; ls; cd $cwd @2020 copyright KalKey training
  • 3. USE OF COLON  Sometimes it is useful to have a command which does “nothing”.  The : (colon) command in Unix does nothing #!/bin/sh if grep unix myfile then : else echo "Sorry, unix was not found" fi @2020 copyright KalKey training
  • 4. THE TEST COMMAND – STRING TESTS  test –z string is string of length 0?  test string1 = string2 does string1 equal string2?  test string1 != string2 not equal?  Example: if test -z $REMOTEHOST then : else DISPLAY="$REMOTEHOST:0" export DISPLAY fi @2020 copyright KalKey training
  • 5. THE TEST COMMAND – INTEGER TESTS Integers can also be compared: – Use -eq, -ne, -lt, -le, -gt, -ge For example: #!/bin/sh smallest=10000 for i in 5 8 19 8 7 3; do if test $i -lt $smallest; then smallest=$i fi done echo $smallest @2020 copyright KalKey training
  • 6. USE OF [ ] The test program has an alias as [ ] – Each bracket must be surrounded by spaces! – This is supposed to be a bit easier to read. For example: #!/bin/sh smallest=10000 for i in 5 8 19 8 7 3; do if [ $i -lt $smallest ] ; then smallest=$i fi done echo $smallest @2020 copyright KalKey training
  • 7. THE WHILE LOOP  While loops repeat statements as long as the next Unix command is successful.  For example: #!/bin/sh i=1 sum=0 while [ $i -le 100 ]; do sum=`expr $sum + $i` i=`expr $i + 1` done echo The sum is $sum. @2020 copyright KalKey training
  • 8. THE UNTIL LOOP  Until loops repeat statements until the next Unix command is successful.  For example: #!/bin/sh x=1 until [ $x -gt 3 ]; do echo x = $x x=`expr $x + 1` done @2020 copyright KalKey training
  • 9. COMMAND LINE ARGUMENTS (1)  Shell scripts would not be very useful if we could not pass arguments to them on the command line  Shell script arguments are “numbered” from left to right – $1 - first argument after command – $2 - second argument after command – ... up to $9 – They are called “positional parameters”. @2020 copyright KalKey training
  • 10. COMMAND LINE ARGUMENTS (2) Example: get a particular line of a file – Write a command with the format: getlineno linenumber filename #!/bin/sh head -$1 $2 | tail -1 Other variables related to arguments: $0 name of the command running $* All the arguments (even if there are more than 9) $# the number of arguments @2020 copyright KalKey training
  • 11. COMMAND LINE ARGUMENTS (3)  Example: print the oldest files in a directory #! /bin/sh # oldest -- examine the oldest parts of a directory HOWMANY=$1 shift ls -lt $* | tail +2 | tail $HOWMANY  The shift command shifts all the arguments to the left – $1 = $2, $2 =$3, $3 = $4, ... – $1 is lost (but we have saved it in $HOWMANY) – The value of $# is changed ($# - 1) – useful when there are more than 9 arguments  The “tail +2” command removes the first line. @2020 copyright KalKey training
  • 12. MORE ON BOURNE SHELL VARIABLES (1) There are three basic types of variables in a shell script: – Positional variables ... $1, $2, $3, ..., $9 – Keyword variables ... Like $PATH, $HOWMANY, and anything else we may define. – Special variables ... @2020 copyright KalKey training
  • 13. MORE ON BOURNE SHELL VARIABLES (2) Special variables: – $*, $# -- all the arguments, the number of the arguments – $$ -- the process id of the current shell – $? -- return value of last foreground process to finish -- more on this one later – There are others you can find out about with man sh @2020 copyright KalKey training
  • 14. READING VARIABLES FROM STANDARD INPUT (1)  The read command reads one line of input from the terminal and assigns it to variables give as arguments  Syntax: read var1 var2 var3 ... Action: reads a line of input from standard input Assign first word to var1, second word to var2, ... The last variable gets any excess words on the line. @2020 copyright KalKey training
  • 15. READING VARIABLES FROM STANDARD INPUT (2)  Example: % read X Y Z Here are some words as input % echo $X Here % echo $Y are % echo $Z some words as input @2020 copyright KalKey training
  • 16. THE CASE STATEMENT  The case statement supports multiway branching based on the value of a single string.  General form: case string in pattern1) command_set_11 ;; pattern2) command_set_2 ;; … esac @2020 copyright KalKey training
  • 17. CASE EXAMPLE #!/bin/sh echo -n 'Choose command [1-4] > ' read reply echo case $reply in "1") date ;; "2"|"3") pwd ;; "4") ls ;; *) echo Illegal choice! ;; esac Use the pipe symbol “|” as a logical or between several choices. Provide a default case when no other cases are matched. @2020 copyright KalKey training
  • 18. REDIRECTION IN BOURNE SHELL SCRIPTS (1)  Standard input is redirected the same (<).  Standard output can be redirected the same (>). – Can also be directed using the notation 1> – For example: cat x 1> ls.txt (only stdout)  Standard error is redirected using the notation 2> – For example: cat x y 1> stdout.txt 2> stderr.txt  Standard output and standard error can be redirected to the same file using the notation 2>&1 – For example: cat x y > xy.txt 2>&1  Standard output and standard error can be piped to the same command using similar notation – For example: cat x y 2>&1 | grep text @2020 copyright KalKey training
  • 19. REDIRECTION IN BOURNE SHELL SCRIPTS (2)  Shell scripts can also supply standard input to commands from text embedded in the script itself.  General form: command << word – Standard input for command follows this line up to, but not including, the line beginning with word.  Example: #!/bin/sh grep 'hello' << EOF This is some sample text. Here is a line with hello in it. Here is another line with hello. No more lines with that word. EOF Only these two lines will be matched and displayed. @2020 copyright KalKey training