Username and Password Validation

LinuxLinuxBeginner
Practice Now

Introduction

In Bash, if statements can be used with multiple conditions. This challenge demonstrates how to define multiple conditions in an if statement using AND logic.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) 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/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_decl("`Variable Declaration`") 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-387359{{"`Username and Password Validation`"}} linux/echo -.-> lab-387359{{"`Username and Password Validation`"}} linux/logical -.-> lab-387359{{"`Username and Password Validation`"}} linux/read -.-> lab-387359{{"`Username and Password Validation`"}} shell/shebang -.-> lab-387359{{"`Username and Password Validation`"}} shell/comments -.-> lab-387359{{"`Username and Password Validation`"}} shell/quoting -.-> lab-387359{{"`Username and Password Validation`"}} shell/variables_decl -.-> lab-387359{{"`Username and Password Validation`"}} shell/variables_usage -.-> lab-387359{{"`Username and Password Validation`"}} shell/cond_expr -.-> lab-387359{{"`Username and Password Validation`"}} shell/read_input -.-> lab-387359{{"`Username and Password Validation`"}} shell/subshells -.-> lab-387359{{"`Username and Password Validation`"}} shell/globbing_expansion -.-> lab-387359{{"`Username and Password Validation`"}} end

Username and Password Validation

Create a Bash script named if_with_AND.sh that prompts the user to enter a username and password. The script will then compare the entered values with the strings "admin" and "secret" respectively. If both values match, the output will be "valid user". Otherwise, the output will be "invalid user".

Requirements

  • The Bash script must be named ~/project/if_with_AND.sh;
  • The script must prompt the user to enter a username and password;
  • The script must use the AND operator (&&) to compare the entered values with "admin" and "secret" respectively;
  • If both values match, the output must be "valid user";
  • Otherwise, the output must be "invalid user".

Example

To run the script, use the following command:

labex:project/ $ bash if_with_AND.sh

Output:

Enter username
admin
Enter password
secret
valid user
labex:project/ $

Summary

This challenge demonstrated how to use the AND operator in an if statement in Bash. By comparing two or more conditions using the && operator, we can create more complex logical statements in our scripts.

Other Linux Tutorials you may like