I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…
In this tutorial, we will see how a switch case block can be used when creating conditional flow depending on the various values of a single expression.
Unix Shell The case-esac Statement
Unix Video #16:
The Shell Switch Case Syntax and Examples:
case <word> in
<first pattern>)
<statements>
;;
<second pattern>)
<statements>
;;
*)
<default statements>
;;
esac
Here, the value of the word expression is matched against each of the choice patterns. If a match is found then the corresponding statements are executed until the ‘;;’ statement is encountered. If there is no match, the default statements under ‘*)’ are executed.
The following is an example of a switch case program:
echo “Enter a number”
read num
case $num in
[0-9])
echo “you have entered a single digit number”
;;
[1-9][1-9])
echo “you have entered a two-digit number”
;;
[1-9][1-9][1-9])
echo “you have entered a three-digit number”
;;
*)
echo “your entry does not match any of the conditions”
;;
Esac
To know more about working with loops in Unix, check out our upcoming tutorial.
Introduction to Unix Shell Scripting: In Unix, the Command Shell is the native command interpreter. It provides a command line interface for the users to interact with the operating system. Unix commands may also be executed non-interactively in the form of a Shell Script. The script is a series of…
Features of Shell Scripting: Unix Variables Shell variables provide us the ability to store and manipulate information within a shell program. In this tutorial, we return to shell scripts and understand how to work with variables. Variables are used to change the flow of the program and to maintain the…
Overview of Pipes in Unix Programming: In this tutorial, we will learn more about Unix Pipes. Later, we will work with some of the remaining filter commands and see an example of piping them together. Unix Video #20: Pipes in Unix A series of filter commands can be piped together…
Learn about the Java Switch Statement, Nested Switch, other variations and usage with the help of simple examples: In this tutorial, we will discuss the Java Switch statement. Here, we will explore each and every concept related to the Switch statement along with the programming examples and their description. You…