10
Most read
11
Most read
12
Most read
Bash Case Statement
The Bash case statement is the simplest form of IF-THEN-ELSE with many ELIF
elements.
Using the case statement makes our bash script more readable and easier to maintain.
These are generally applied to simplify the complex conditions having multiple
different choices.
The Bash case statement follows a similar logic as the Javascript or C switch
statement. There is a slight difference, as follows:
The Bash case statement takes a value once and tests that value multiple times. It
stops searching for a pattern once it has found it and executed the statement linked with
it, which is almost opposite in case of the C switch statement.
Bash Case Statement Conti…
Case Statement Syntax
Patterns
A pattern and its commands make a clause, which
ends with ;;.
Patterns support special characters.
The ) terminates a pattern list.
The | operator separates multiple patterns.
case expression in
pattern_1)
statements
;;
pattern_2)
statements
;;
pattern_3|pattern_4|pattern_5)
statements
;;
pattern-n)
statements
;;
*)
statements
;;
esac
Bash Case Statement Conti…
There are some key points of bash case statements:
Each case statement in bash starts with the 'case' keyword, followed by the case
expression and 'in' keyword.
 The case statement is closed by 'esac' keyword.
We can apply multiple patterns separated by | operator. The ) operator indicates the
termination of a pattern list.
A pattern containing the statements is referred to as a clause, and it must be
terminated by double semicolon (;;).
An asterisk symbol (*) is used as a final pattern to define the default case.
 It is used as a default case when used as the last case.
Bash Case Statement Conti…
Example: #!/bin/bash
echo "Do you know Unix Operating System?"
read -p "Yes/No? :" Answer
case $Answer in
Yes|yes|y|Y)
echo "That's amazing."
echo
;;
No|no|N|n)
echo "It's easy. Let's start learning from Online Platform."
;;
esac
#!/bin/bash
echo "enter a and b"
read a
Bash Case Statement Conti…
Example:To Perform Arithmetic Operation
#!/bin/sh
echo "enter a and b"
read a
read b
echo "enter operation to be performed"
read op
case $op in
+) c=`expr $a + $b` ;;
-) c=`expr $a - $b` ;;
/) c=`expr $a / $b` ;;
*) c=`expr $a * $b` ;;
*) echo "no valid operation specified" ;;
esac
echo Result after performing operation on a and b is
echo $c
Loop
#1) Unix For loop statement
Sytax:
For varname in list
do
Echo “Statement”
done
Keywords are for, in, do, done
List is a list of variables which are separated by spaces. If list is not mentioned in the
for statement, then it takes the positional parameter value that were passed into the
shell.
Varname is any variable assumed by the user.
Example: Print table of 2. using for loop
#!/bin/sh
for num in {2..20.2}
do
Echo “Table of 2 : $num”
done
Loop
Syntax of for like C programming language.
Syntax: for (( cond1; cond2; cond3 ))
do
echo “Statement”
done
Look at the above snapshot, condition1 indicates initialization, cond2
indicates condition and cond3 indicates updation.
#!/bash/sh
For (( num=1; num<=10; num++ ))
do
echo “$num”
done
Loop
#2) while loop:
Linux scripting while loop is similar to C language while loop. There is a condition in
while. And commands are executed till the condition is valid. Once condition becomes
false, loop terminates.
Syntax: while [condition]
do
commands
done
Example: printing number in reverse order. [10 to 0]
#!/bin/sh
i=10;
while [ $i -le 0 ]
do
echo “$i”
let i-- ;
done
while infinite loop:
Loop
while infinite loop:
Infinite loop is also called endless loop. It is made with while true (it means condition
will always be true) or while : (it means an empty expression), where colon (:) is
equivalent to no operation.
Example:
#!/bash/sh
while true or while :
do
Echo “This is infinite loop. Please ctrl + c to Exit.”
done
Loop
#3) Until loop:
It is similar to while loop. The only difference is that until statement executes its code
block while its conditional expression is false, and while statement executes its code
block while its conditional expression is true.
Syntax: until [ condition ];
do
commands
done
Example: example to display number from 5 to 15.
#!/bin/sh
i=5;
Until [ $i –gt 15 ];
do
Echo “$i”
i=$(( i+1 ))
done
Command line argument
The Unix shell is used to run commands, and it allows users to pass run time
arguments to these commands.
These arguments, also known as command line parameters, that allows the users to
either control the flow of the command or to specify the input data for the command.
While running a command, the user can pass a variable number of parameters in the
command line.
Within the command script, the passed parameters are accessible using ‘positional
parameters’. These range from $0 to $9, where $0 refers to the name of the
command itself, and $1 to $9 are the first through to the ninth parameter, depending
on how many parameters were actually passed.
Example:
$ Hello student this is command line argement
Here $0 would be assigned sh
$1 would be assigned Hello
$2 would be assigned student
And so on …
We will now look at some additional commands to process these parameters.
#1) set
Command line argument
We will now look at some additional commands to process these parameters.
#1) set
This command can be used to set the values of the positional parameters on the
command line.
Example:
$ set Welcome to Unix World
$ echo $1 $2
Welcome to
Here, “Welcome” was assigned to $1 and “to” was assigned to $2 and so on.
#2) shift
This command is used to shift the position of the positional parameters. i.e. $2 will be
shifted to $1 all the way to the tenth parameter being shifted to $9. Note that if in case
there are more than 9 parameters, this mechanism can be used to read beyond the 9th.
Example:
$ set hello good morning how do you do welcome to Unix tutorial.
Here, ‘hello’ is assigned to $1, ‘good’ to $2 and so on to ‘to’ being assigned to $9. Now
the shift command can be used to shift the parameters ‘N’ places.
Command line argument
Example:
$ shift 2 $ echo $1Now $1 will be ‘morning’ and so on to $8 being ‘unix’ and $9 being
‘tutorial’.

More Related Content

PDF
Operating_System_Lab_ClassOperating_System_2.pdf
PPTX
Unix Shell Programming subject shell scripting ppt
PPTX
PHP Powerpoint -- Teach PHP with this
PPT
Shell programming
PPTX
First steps in C-Shell
PPTX
Php Loop
PPT
Shell Scripting
PPTX
advancing in php programming part four.pptx
Operating_System_Lab_ClassOperating_System_2.pdf
Unix Shell Programming subject shell scripting ppt
PHP Powerpoint -- Teach PHP with this
Shell programming
First steps in C-Shell
Php Loop
Shell Scripting
advancing in php programming part four.pptx

Similar to Case, Loop & Command line args un Unix (20)

PPT
2-introduction_to_shell_scripting
PPTX
Linux System Administration
DOCX
What is the general format for a Try-Catch block Assume that amt l .docx
PPT
ShellProgramming and Script in operating system
PDF
Advanced perl finer points ,pack&amp;unpack,eval,files
PPTX
Licão 11 decision making - statement
PPTX
Switch case and looping
PDF
Shell scripting _how_to_automate_command_l_-_jason_cannon
PPT
Ruby Basics
 
PPTX
Licão 12 decision loops - statement iteration
PPTX
Switch case and looping jam
PPTX
Switch case and looping new
PPT
34-shell-programming asda asda asd asd.ppt
DOCX
Vb script tutorial
PPT
CPAP.com Introduction To Coding: Part 2
PDF
Module 03 Programming on Linux
PPTX
Macasu, gerrell c.
PDF
A Quick Taste of C
PDF
Vb script tutorial
PPT
Best training-in-mumbai-shell scripting
2-introduction_to_shell_scripting
Linux System Administration
What is the general format for a Try-Catch block Assume that amt l .docx
ShellProgramming and Script in operating system
Advanced perl finer points ,pack&amp;unpack,eval,files
Licão 11 decision making - statement
Switch case and looping
Shell scripting _how_to_automate_command_l_-_jason_cannon
Ruby Basics
 
Licão 12 decision loops - statement iteration
Switch case and looping jam
Switch case and looping new
34-shell-programming asda asda asd asd.ppt
Vb script tutorial
CPAP.com Introduction To Coding: Part 2
Module 03 Programming on Linux
Macasu, gerrell c.
A Quick Taste of C
Vb script tutorial
Best training-in-mumbai-shell scripting
Ad

More from Vpmv (11)

PPT
Operating System.ppt
PDF
Classification of Computers.pdf
PDF
Basic Components of Computer....pdf
PDF
Algorithm & Flowchart.pdf
PDF
Process & Thread Management
PDF
Evolution or Generation of OS.pdf
PDF
Introduction of OS & Classification of Software
PPTX
FILE PERMISSION OR ACCESS MODE
PPTX
Directory Management in Unix
PPTX
Stucture of c program
PPT
Network topology
Operating System.ppt
Classification of Computers.pdf
Basic Components of Computer....pdf
Algorithm & Flowchart.pdf
Process & Thread Management
Evolution or Generation of OS.pdf
Introduction of OS & Classification of Software
FILE PERMISSION OR ACCESS MODE
Directory Management in Unix
Stucture of c program
Network topology
Ad

Recently uploaded (20)

PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PPTX
Module on health assessment of CHN. pptx
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
Complications of Minimal Access-Surgery.pdf
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
PDF
Empowerment Technology for Senior High School Guide
PDF
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
semiconductor packaging in vlsi design fab
PDF
IP : I ; Unit I : Preformulation Studies
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
Module on health assessment of CHN. pptx
Introduction to pro and eukaryotes and differences.pptx
AI-driven educational solutions for real-life interventions in the Philippine...
Complications of Minimal Access-Surgery.pdf
Literature_Review_methods_ BRACU_MKT426 course material
Core Concepts of Personalized Learning and Virtual Learning Environments
Empowerment Technology for Senior High School Guide
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
What’s under the hood: Parsing standardized learning content for AI
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Journal of Dental Science - UDMY (2021).pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
semiconductor packaging in vlsi design fab
IP : I ; Unit I : Preformulation Studies
Unit 4 Computer Architecture Multicore Processor.pptx
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf

Case, Loop & Command line args un Unix

  • 1. Bash Case Statement The Bash case statement is the simplest form of IF-THEN-ELSE with many ELIF elements. Using the case statement makes our bash script more readable and easier to maintain. These are generally applied to simplify the complex conditions having multiple different choices. The Bash case statement follows a similar logic as the Javascript or C switch statement. There is a slight difference, as follows: The Bash case statement takes a value once and tests that value multiple times. It stops searching for a pattern once it has found it and executed the statement linked with it, which is almost opposite in case of the C switch statement.
  • 2. Bash Case Statement Conti… Case Statement Syntax Patterns A pattern and its commands make a clause, which ends with ;;. Patterns support special characters. The ) terminates a pattern list. The | operator separates multiple patterns. case expression in pattern_1) statements ;; pattern_2) statements ;; pattern_3|pattern_4|pattern_5) statements ;; pattern-n) statements ;; *) statements ;; esac
  • 3. Bash Case Statement Conti… There are some key points of bash case statements: Each case statement in bash starts with the 'case' keyword, followed by the case expression and 'in' keyword.  The case statement is closed by 'esac' keyword. We can apply multiple patterns separated by | operator. The ) operator indicates the termination of a pattern list. A pattern containing the statements is referred to as a clause, and it must be terminated by double semicolon (;;). An asterisk symbol (*) is used as a final pattern to define the default case.  It is used as a default case when used as the last case.
  • 4. Bash Case Statement Conti… Example: #!/bin/bash echo "Do you know Unix Operating System?" read -p "Yes/No? :" Answer case $Answer in Yes|yes|y|Y) echo "That's amazing." echo ;; No|no|N|n) echo "It's easy. Let's start learning from Online Platform." ;; esac #!/bin/bash echo "enter a and b" read a
  • 5. Bash Case Statement Conti… Example:To Perform Arithmetic Operation #!/bin/sh echo "enter a and b" read a read b echo "enter operation to be performed" read op case $op in +) c=`expr $a + $b` ;; -) c=`expr $a - $b` ;; /) c=`expr $a / $b` ;; *) c=`expr $a * $b` ;; *) echo "no valid operation specified" ;; esac echo Result after performing operation on a and b is echo $c
  • 6. Loop #1) Unix For loop statement Sytax: For varname in list do Echo “Statement” done Keywords are for, in, do, done List is a list of variables which are separated by spaces. If list is not mentioned in the for statement, then it takes the positional parameter value that were passed into the shell. Varname is any variable assumed by the user. Example: Print table of 2. using for loop #!/bin/sh for num in {2..20.2} do Echo “Table of 2 : $num” done
  • 7. Loop Syntax of for like C programming language. Syntax: for (( cond1; cond2; cond3 )) do echo “Statement” done Look at the above snapshot, condition1 indicates initialization, cond2 indicates condition and cond3 indicates updation. #!/bash/sh For (( num=1; num<=10; num++ )) do echo “$num” done
  • 8. Loop #2) while loop: Linux scripting while loop is similar to C language while loop. There is a condition in while. And commands are executed till the condition is valid. Once condition becomes false, loop terminates. Syntax: while [condition] do commands done Example: printing number in reverse order. [10 to 0] #!/bin/sh i=10; while [ $i -le 0 ] do echo “$i” let i-- ; done while infinite loop:
  • 9. Loop while infinite loop: Infinite loop is also called endless loop. It is made with while true (it means condition will always be true) or while : (it means an empty expression), where colon (:) is equivalent to no operation. Example: #!/bash/sh while true or while : do Echo “This is infinite loop. Please ctrl + c to Exit.” done
  • 10. Loop #3) Until loop: It is similar to while loop. The only difference is that until statement executes its code block while its conditional expression is false, and while statement executes its code block while its conditional expression is true. Syntax: until [ condition ]; do commands done Example: example to display number from 5 to 15. #!/bin/sh i=5; Until [ $i –gt 15 ]; do Echo “$i” i=$(( i+1 )) done
  • 11. Command line argument The Unix shell is used to run commands, and it allows users to pass run time arguments to these commands. These arguments, also known as command line parameters, that allows the users to either control the flow of the command or to specify the input data for the command. While running a command, the user can pass a variable number of parameters in the command line. Within the command script, the passed parameters are accessible using ‘positional parameters’. These range from $0 to $9, where $0 refers to the name of the command itself, and $1 to $9 are the first through to the ninth parameter, depending on how many parameters were actually passed. Example: $ Hello student this is command line argement Here $0 would be assigned sh $1 would be assigned Hello $2 would be assigned student And so on … We will now look at some additional commands to process these parameters. #1) set
  • 12. Command line argument We will now look at some additional commands to process these parameters. #1) set This command can be used to set the values of the positional parameters on the command line. Example: $ set Welcome to Unix World $ echo $1 $2 Welcome to Here, “Welcome” was assigned to $1 and “to” was assigned to $2 and so on. #2) shift This command is used to shift the position of the positional parameters. i.e. $2 will be shifted to $1 all the way to the tenth parameter being shifted to $9. Note that if in case there are more than 9 parameters, this mechanism can be used to read beyond the 9th. Example: $ set hello good morning how do you do welcome to Unix tutorial. Here, ‘hello’ is assigned to $1, ‘good’ to $2 and so on to ‘to’ being assigned to $9. Now the shift command can be used to shift the parameters ‘N’ places.
  • 13. Command line argument Example: $ shift 2 $ echo $1Now $1 will be ‘morning’ and so on to $8 being ‘unix’ and $9 being ‘tutorial’.