Lucky Number Prize Checker

LinuxLinuxBeginner
Practice Now

Introduction

Bash is a popular shell used in Linux and Unix operating systems. It provides a powerful command-line interface for users to interact with the system. In this challenge, we will explore the use of the case statement in Bash scripting.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/SystemInformationandMonitoringGroup -.-> linux/time("`Command Timing`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/case("`Case Statements`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/echo -.-> lab-387362{{"`Lucky Number Prize Checker`"}} linux/read -.-> lab-387362{{"`Lucky Number Prize Checker`"}} linux/time -.-> lab-387362{{"`Lucky Number Prize Checker`"}} shell/shebang -.-> lab-387362{{"`Lucky Number Prize Checker`"}} shell/comments -.-> lab-387362{{"`Lucky Number Prize Checker`"}} shell/quoting -.-> lab-387362{{"`Lucky Number Prize Checker`"}} shell/variables_usage -.-> lab-387362{{"`Lucky Number Prize Checker`"}} shell/case -.-> lab-387362{{"`Lucky Number Prize Checker`"}} shell/for_loops -.-> lab-387362{{"`Lucky Number Prize Checker`"}} shell/read_input -.-> lab-387362{{"`Lucky Number Prize Checker`"}} shell/globbing_expansion -.-> lab-387362{{"`Lucky Number Prize Checker`"}} linux/wildcard -.-> lab-387362{{"`Lucky Number Prize Checker`"}} end

Lucky Number Prize Checker

You need to write a Bash script that takes user input for their lucky number and outputs a message based on the number entered. If the number entered matches a specific value, the script will output a prize message. If the number entered does not match any of the specific values, the script will output a message to try again next time.

Requirements

  • Create a new file named ~/project/case_example.sh.
  • Use the #!/bin/bash shebang at the beginning of the script.
  • Prompt the user to enter their lucky number.
  • Use the case statement to check the value of the user's input.
  • If the input matches 101, output "You got 1st prize".
  • If the input matches 510, output "You got 2nd prize".
  • If the input matches 999, output "You got 3rd prize".
  • If the input does not match any of the above values, output "Sorry, try for the next time".

Example

To run the script, use the following command:

bash case_example.sh

Output:

Enter your lucky number
101
You got 1st prize

Summary

In this challenge, we learned how to use the case statement in Bash scripting. The case statement provides an alternative to the if-elseif-else statement and can be used to check multiple values of a variable. By using the case statement, we were able to create a script that outputs a message based on the user's input.

Other Linux Tutorials you may like