SlideShare a Scribd company logo
1 of 19
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 shellxylas121
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 
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 LearningASIT Education
 
Ruby programming introduction
Ruby programming introductionRuby programming introduction
Ruby programming introductionASIT Education
 
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functionsDocent Education
 
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,filesShankar 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 ProgrammingHoàng Lâm Huỳnh
 
Perl one-liners
Perl one-linersPerl one-liners
Perl one-linersdaoswald
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awkYogesh Sawant
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programmingkayalkarnan
 
Namespace--defining same identifiers again
Namespace--defining same identifiers againNamespace--defining same identifiers again
Namespace--defining same identifiers againAjay Chimmani
 
Quize on scripting shell
Quize on scripting shellQuize on scripting shell
Quize on scripting shelllebse123
 
Shell Scripts
Shell ScriptsShell Scripts
Shell ScriptsDr.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.pptKiranMantri
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaewout2
 
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_manualKuntal 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
 
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 VariablesGaurav Bisht
 
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 Foobrian_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 1Leandro Lima
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting languageVamshi 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 swarmKalkey
 
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
 
Sonarqube
SonarqubeSonarqube
SonarqubeKalkey
 
Introduction of tomcat
Introduction of tomcatIntroduction of tomcat
Introduction of tomcatKalkey
 
Jenkins advance topic
Jenkins advance topicJenkins advance topic
Jenkins advance topicKalkey
 
Jenkins introduction
Jenkins introductionJenkins introduction
Jenkins introductionKalkey
 
Terraform day 3
Terraform day 3Terraform day 3
Terraform day 3Kalkey
 
Terraform day 2
Terraform day 2Terraform day 2
Terraform day 2Kalkey
 
Terraform day 1
Terraform day 1Terraform day 1
Terraform day 1Kalkey
 
Ansible day 3
Ansible day 3Ansible day 3
Ansible day 3Kalkey
 
Cloud computing 1
Cloud computing  1Cloud computing  1
Cloud computing 1Kalkey
 
Adnible day 2.ppt
Adnible day   2.pptAdnible day   2.ppt
Adnible day 2.pptKalkey
 
Debasihish da final.ppt
Debasihish da final.pptDebasihish da final.ppt
Debasihish da final.pptKalkey
 
Linux day 3ppt
Linux day 3pptLinux day 3ppt
Linux day 3pptKalkey
 
Ansible day 1.ppt
Ansible day 1.pptAnsible day 1.ppt
Ansible day 1.pptKalkey
 
Linux day 2.ppt
Linux day  2.pptLinux day  2.ppt
Linux day 2.pptKalkey
 
Docker advance topic
Docker advance topicDocker advance topic
Docker advance topicKalkey
 

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

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

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