Bash Script Lucky Number Checker

LinuxLinuxBeginner
Practice Now

Introduction

Bash is a popular Unix shell and command language. It is used to automate repetitive tasks, manage system configurations, and execute complex scripts. In this challenge, we will explore how to use the else if statement in Bash.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) 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/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") 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/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} linux/echo -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} linux/read -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} linux/time -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} shell/shebang -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} shell/comments -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} shell/quoting -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} shell/variables_usage -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} shell/for_loops -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} shell/cond_expr -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} shell/read_input -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} shell/globbing_expansion -.-> lab-387361{{"`Bash Script Lucky Number Checker`"}} end

Bash Script Lucky Number Checker

The problem is to create a Bash script that uses the else if statement to check a user's lucky number and award prizes accordingly. The script should prompt the user to enter their lucky number and then check it against a set of predefined values. If the number matches one of the values, the script should print a message indicating the prize the user has won. If the number does not match any of the values, the script should print a message indicating that the user did not win a prize.

Requirements

  • The script should be named ~/project/elseif_example.sh.
  • The script should use the else if statement to check the user's lucky number.
  • The script should prompt the user to enter their lucky number.
  • The script should check the user's lucky number against the following values:
    • 101: 1st prize
    • 510: 2nd prize
    • 999: 3rd prize
  • If the user's lucky number matches one of the values, the script should print a message indicating the prize the user has won.
  • If the user's lucky number does not match any of the values, the script should print a message indicating that the user did not win a prize.

Example

To run the script, use the following command:

bash elseif_example.sh

Output:

Enter your lucky number
101
You got 1st prize

Summary

In this challenge, we learned how to use the else if statement in Bash to check a user's lucky number and award prizes accordingly. We also learned how to prompt the user for input and how to use conditional statements to control the flow of the script. With these skills, we can create more complex Bash scripts to automate tasks and manage system configurations.

Other Linux Tutorials you may like