Interview Questions

Shell Script - Process Tasks based on User Input

In this section, let us see how to get User Input and execute different tasks based on the User Input data.

In the below article, let us see the following,
1. How to get Input from User
2. How to process the Input i e., a simulation is created so that you can use the below piece of code where ever applicable.

1. User Input:
The list of options available in our application is displayed as options so that User can select the appropriate one.

2. We have used the case statements, hence based on the User selection respective case statement block gets executed. Hence if we place our logic in that particular section, our code gets executed.

Note: Based on your requirement, change the options values and place your code logic in the respective case block. Now your task will be simple,

By this way, you can easily process User Input and your effort in developing is made easy !

ReadUserInputAndProcess.sh

# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

#
# Program to get User Input and Process using Switch statement
#

echo "Note: The below data are for simulation only as if you were getting input from User for a specific task"
echo "The below logic defines that"
echo "1. You get the input from User for a specific tasks to be performed on your script"
echo "2. Based on the selection, respective section gets executed"
echo "3. Place your Code in the appropriate section and your logic gets executed"
echo "4. For more options, just follow the below logic and add more options."
echo ""

echo "Enter your choice from the below options"
echo "1. Start"
echo "2. Stop"
echo "3. Exit"

echo "Enter the Option No. "
read input_name

case ${input_name} in
        1) echo "Result :: You have selected to Start the application"
                # Place your processing logic here
                ;;

        2) echo "Result :: You have selected to Stopt the application"
                # Place your processing logic here
                ;;

        3) echo "Result :: You have selected to Exit"
                # Place your processing logic here
                ;;

        *) echo "Result :: You have selected Wrong data. Please select the right one"
                # Place your processing logic here
                exit 1
                ;;
esac

In the above script, you need place your code logic in the case block. If your logic is a complex one, then placing the entire code within the case block would make script to be a complex one. Hence, we have introduced the methods in the case block hence you can place your processing logic in the appropriate methods so that you script look well formatted and easy readability. By this way, you can also learn how to use methods in Shell script.

ReadUserInputAndProcess.sh

# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

#
# Program to get User Input and Process using Switch statement
#

echo "Note: The below data are for simulation only as if you were getting input from User for a specific task"
echo "The below logic defines that"
echo "1. You get the input from User for a specific tasks to be performed on your script"
echo "2. Based on the selection, respective section gets executed"
echo "3. Place your Code in the appropriate section and your logic gets executed"
echo "4. For more options, just follow the below logic and add more options."
echo ""

process_Start()
{
# Place your processing logic here
echo "Result :: You have selected to Start the application"
}

process_Stop()
{
# Place your processing logic here
echo "Result :: You have selected to Stop the application"
}

process_Exit()
{
# Place your processing logic here
echo "Result :: You have selected to Exit the menu options"
}

echo "Enter your choice from the below options"
echo "1. Start"
echo "2. Stop"
echo "3. Exit"

echo "Enter the Option No. "
read input_name

case ${input_name} in
1) process_Start
;;

2) process_Stop
;;

3) process_Exit
;;

*) echo "Result :: You have selected Wrong data. Please select the right one"
# Place your processing logic here
exit 1
;;
esac