Random Password Generator Using Shell Commands

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will write a script to generate random 8-character passwords that include alpha-numeric and special characters. We will use the rand, tr, and cut commands along with the /dev/urandom file to generate random values. The script will prompt the user to enter the number of passwords they want to generate and then output the randomly generated passwords.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) 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/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/TextProcessingGroup -.-> linux/tr("`Character Translating`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") 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/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/cat -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} linux/head -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} linux/echo -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} linux/pipeline -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} linux/redirect -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} linux/read -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} linux/tr -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} linux/cd -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} shell/shebang -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} shell/comments -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} shell/quoting -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} shell/variables_usage -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} shell/cond_expr -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} shell/read_input -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} shell/subshells -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} shell/adv_redirection -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} shell/globbing_expansion -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} linux/wildcard -.-> lab-18299{{"`Random Password Generator Using Shell Commands`"}} end

Random Password Generator Challenge

In this challenge, you are required to write a script for generating random 8-character passwords that include alphanumeric and special characters.

Tasks

You need to complete the following task:

  1. Write a script for generating random 8-character passwords.

Requirements

You must meet the following requirements:

  • The script should be named randPasswd.sh.
  • Work directory should be ~/project.
  • Knowledge about rand, tr, and cut commands is required.
  • Use of /dev/urandom file.
  • Piping should be implemented.

Example

labex:project/ $ bash randPasswd.sh
[INPUT] Enter the Number of Password to Generate [INPUT]
4
[OUTPUT] Random Generated Password is [OUTPUT]
UY)UsN0l
)4S1gtEK
BSQCp7pO
{&9VQoKX

Summary

After completing this challenge, you will have learned how to generate random values using the /dev/urandom file and how to use the rand, tr, and cut commands to manipulate and format the output. You will also have practiced piping between multiple commands and prompting the user for input. This challenge will help you improve your scripting skills and give you a better understanding of how to generate random passwords in a secure and efficient way.

Other Linux Tutorials you may like