How to perform different operations based on user input?

Performing Different Operations Based on User Input

In the world of shell scripting, the ability to perform different operations based on user input is a crucial skill. This allows your scripts to adapt to the user's needs, making them more dynamic and versatile. In this response, we'll explore how to achieve this functionality in a Linux environment.

Accepting User Input

The first step in performing different operations based on user input is to capture the user's input. In shell scripting, you can use the read command to prompt the user for input and store it in a variable. Here's an example:

echo "Enter your name: "
read name
echo "Hello, $name!"

In this example, the script prompts the user to enter their name, stores the input in the name variable, and then prints a greeting using the user's name.

Conditional Statements

Once you have the user's input, you can use conditional statements to perform different operations based on the value of the input. The most common conditional statements in shell scripting are if-else and case.

if-else Statements

The if-else statement allows you to execute different code blocks based on a condition. Here's an example:

echo "Enter a number: "
read num

if [ $num -gt 0 ]; then
    echo "The number is positive."
else
    echo "The number is non-positive."
fi

In this example, the script prompts the user to enter a number, and then checks if the number is greater than 0. If the condition is true, it prints a message indicating that the number is positive; otherwise, it prints a message indicating that the number is non-positive.

case Statements

The case statement is useful when you have multiple conditions to check. It provides a more concise and readable way to handle different scenarios. Here's an example:

echo "Enter your favorite color (red, green, or blue): "
read color

case $color in
    "red")
        echo "Red is a warm color."
        ;;
    "green")
        echo "Green is a cool color."
        ;;
    "blue")
        echo "Blue is a calming color."
        ;;
    *)
        echo "That's an interesting color, but it's not one of the options."
        ;;
esac

In this example, the script prompts the user to enter their favorite color, and then uses a case statement to execute different code blocks based on the user's input. If the user enters "red", "green", or "blue", the script prints a message about that color. If the user enters any other value, the script prints a message indicating that the color is not one of the options.

Combining Conditional Statements

You can also combine multiple conditional statements to create more complex logic. For example, you can use an if-elif-else structure to handle different scenarios:

echo "Enter a number: "
read num

if [ $num -gt 0 ]; then
    echo "The number is positive."
elif [ $num -lt 0 ]; then
    echo "The number is negative."
else
    echo "The number is zero."
fi

In this example, the script first checks if the number is greater than 0, then if it's less than 0, and finally if it's equal to 0.

Visualizing the Concept

Here's a Mermaid diagram that illustrates the flow of a script that performs different operations based on user input:

graph TD A[Start] --> B[Prompt user for input] B --> C{Check user input} C -->|Condition 1| D[Perform Operation 1] C -->|Condition 2| E[Perform Operation 2] C -->|Condition 3| F[Perform Operation 3] C -->|Default| G[Perform Default Operation] D --> H[End] E --> H F --> H G --> H

In this diagram, the script starts by prompting the user for input. It then checks the user's input and performs different operations based on the input. If none of the conditions match, the script performs a default operation.

Real-World Example

Imagine you're running a small online store, and you want to create a script that helps customers calculate the total cost of their order based on the number of items they want to purchase. Here's an example:

echo "Welcome to our online store!"
echo "Please enter the number of items you want to purchase: "
read items

if [ $items -lt 1 ]; then
    echo "You must purchase at least one item."
elif [ $items -lt 11 ]; then
    total=$((items * 10))
    echo "The total cost of your order is $total."
else
    total=$((items * 8))
    echo "The total cost of your order is $total."
fi

In this example, the script first prompts the user to enter the number of items they want to purchase. It then uses an if-elif-else structure to perform different calculations based on the user's input:

  • If the user enters a number less than 1, the script prints a message indicating that they must purchase at least one item.
  • If the user enters a number between 1 and 10 (inclusive), the script calculates the total cost by multiplying the number of items by $10 per item.
  • If the user enters a number greater than 10, the script calculates the total cost by multiplying the number of items by $8 per item.

This example demonstrates how you can use conditional statements to create a dynamic and user-friendly script that adapts to the customer's needs.

In conclusion, the ability to perform different operations based on user input is a fundamental skill in shell scripting. By using conditional statements like if-else and case, you can create scripts that are more versatile and responsive to the user's needs. Remember to experiment, practice, and explore the vast world of shell scripting to become a more proficient programmer.

0 Comments

no data
Be the first to share your comment!