Bash Script with OR Logic

LinuxLinuxBeginner
Practice Now

Introduction

In this challenge, we will learn how to use the OR logic in the if statement of Bash.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) 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/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/BasicSystemCommandsGroup -.-> linux/logical("`Logic Operations`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") 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/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-387360{{"`Bash Script with OR Logic`"}} linux/echo -.-> lab-387360{{"`Bash Script with OR Logic`"}} linux/pipeline -.-> lab-387360{{"`Bash Script with OR Logic`"}} linux/logical -.-> lab-387360{{"`Bash Script with OR Logic`"}} linux/read -.-> lab-387360{{"`Bash Script with OR Logic`"}} shell/shebang -.-> lab-387360{{"`Bash Script with OR Logic`"}} shell/comments -.-> lab-387360{{"`Bash Script with OR Logic`"}} shell/quoting -.-> lab-387360{{"`Bash Script with OR Logic`"}} shell/variables_usage -.-> lab-387360{{"`Bash Script with OR Logic`"}} shell/cond_expr -.-> lab-387360{{"`Bash Script with OR Logic`"}} shell/read_input -.-> lab-387360{{"`Bash Script with OR Logic`"}} shell/subshells -.-> lab-387360{{"`Bash Script with OR Logic`"}} shell/globbing_expansion -.-> lab-387360{{"`Bash Script with OR Logic`"}} end

Bash Script with OR Logic

Create a Bash script named ~/project/if_with_OR.sh that takes a number input from the user. If the value is equal to 15 or 45, then the output will be "You won the game". Otherwise, the output will be "You lost the game".

Requirements

  • Bash shell
  • Text editor

Example

To run the script, use the following command:

bash if_with_OR.sh

Output:

Enter any number
15
You won the game

Summary

In this challenge, we learned how to use the OR logic in the if statement of Bash. We created a Bash script that takes a number input from the user and checks if it is equal to 15 or 45. If it is, then the output is "You won the game". Otherwise, the output is "You lost the game".

Other Linux Tutorials you may like