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.
Overview of Unix Shell Loops and Different Loop Types like: Unix Do While Loop Unix For Loop Unix Until Loop In this tutorial, we will cover the control instructions that are used to iterate a set of commands over a series of data. Unix offers three loop structures of which we can…
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 Advanced Unix Shell Scripting: Unix shells provide a very powerful and extensible framework for scripting. A number of advanced mechanisms allow for more complex scripting using the inbuilt commands. Unix Video #24: Advanced Shell Scripting in Unix Unix includes commands for: Testing various conditions associated with specified files.…
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…
Working with Unix Vi Editor Vi is the standard editor that is available on Unix systems. This visual editor enables manipulation of text while showing a screenful of text unlike ‘sed’ which works at the line level. Unix Video #12 Modes of Unix Vi Editor The vi editor has three…
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…
Introduction to Unix Operating System: Let's start with Tutorial #1: 'What is Unix' in this series. In this tutorial, you will be able to understand the basic concepts of operating systems, the features of Unix, along its Architecture. => Click here for the Complete Unix Tutorial series Unix Video #1:…