SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Bash shell
Bash shellBash shell
Bash shell
xylas121
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 
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
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
kayalkarnan
 
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
 

Tendances (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
 

Similaire à Shell programming 2

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
 
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
 

Similaire à 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
 

Plus de Gourav Varma (20)

Jenkins introduction
Jenkins introductionJenkins introduction
Jenkins introduction
 
Docker introduction (1)
Docker introduction (1)Docker introduction (1)
Docker introduction (1)
 
Aws day 4
Aws day 4Aws day 4
Aws day 4
 
Aws day 3
Aws day 3Aws day 3
Aws day 3
 
Aws day 2
Aws day 2Aws day 2
Aws day 2
 
Ansible day 4
Ansible day 4Ansible day 4
Ansible day 4
 
Ansible day 3
Ansible day 3Ansible day 3
Ansible day 3
 
Adnible day 2.ppt
Adnible day   2.pptAdnible day   2.ppt
Adnible day 2.ppt
 
Ansible day 1.ppt
Ansible day 1.pptAnsible day 1.ppt
Ansible day 1.ppt
 
Version control git day03(amarnath dada)
Version control   git day03(amarnath dada)Version control   git day03(amarnath dada)
Version control git day03(amarnath dada)
 
Version control git day02
Version control   git day02Version control   git day02
Version control git day02
 
Version control git day01
Version control   git day01Version control   git day01
Version control git day01
 
Dev ops
Dev opsDev ops
Dev ops
 
Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linux
 
Final terraform
Final terraformFinal terraform
Final terraform
 
Version control git day03
Version control   git day03Version control   git day03
Version control git day03
 
Version control git day02
Version control   git day02Version control   git day02
Version control git day02
 
Version control git day01
Version control   git day01Version control   git day01
Version control git day01
 
Docker swarm
Docker swarmDocker swarm
Docker swarm
 
Docker advance topic (2)
Docker advance topic (2)Docker advance topic (2)
Docker advance topic (2)
 

Dernier

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Dernier (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 

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