#!/bin/sh echo "###############################" echo " Welcome to password generator " echo "###############################" # i) feature to show the strength of the password generated. For example, week, strong, very strong, etc. # ii) to give choices to users to include either letters (upper/lower case) or digits or symbols in the password # iii) to only generate password that is not previously generated to other users # iv) feature to generate bulk of N passwords according to user's input size of N. # v) send the generated password to the clipboard on both Unix and Mac OSx so that user can copy and # store the password. # vi) implement a feature to generate another password if # previously generated password is forgotten # by a specific user. # vii) to write the script so that it can be run in both Linux and Mac OSx. # display the menu echo "1. Generate a password" echo "2. Generate bulk of passwords" echo "3. Generate a password for a specific user" echo "4. Show password strength" echo "Q. Exit" # read the input from the keyboard and assign it to a variable read -p "Enter your choice: " choice # validate the input case $choice in 1) echo "Generating a password" # generate a password # ask user to enter the password length read -p "Enter the password length: " length # ask user to add lowercase letters or uppercase letters or digits or symbols to the password read -p "Do you want to add lowercase letters? (y/n): " lowercase read -p "Do you want to add uppercase letters? (y/n): " uppercase read -p "Do you want to add digits? (y/n): " digits read -p "Do you want to add symbols? (y/n): " symbols # generate the password password="" while [ ${#password} -lt $length ] do if [ $lowercase = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' 'a-z') fi if [ $uppercase = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' 'A-Z') fi if [ $digits = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' '0-9') fi if [ $symbols = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' '!@#$%^&*()') fi done # display the password and copy it to the clipboard echo "Your password is: $password" echo $password | pbcopy echo "Password copied to clipboard" exit ;; 2) echo "Generating bulk of passwords" # ask user to enter the number of passwords to be generated read -p "Enter the number of passwords to be generated: " number # ask user to enter the password length read -p "Enter the password length: " length # ask user to add lowercase letters or uppercase letters or digits or symbols to the password read -p "Do you want to add lowercase letters? (y/n): " lowercase read -p "Do you want to add uppercase letters? (y/n): " uppercase read -p "Do you want to add digits? (y/n): " digits read -p "Do you want to add symbols? (y/n): " symbols # generate the passwords for (( i=1; i<=$number; i++ )) do password="" while [ ${#password} -lt $length ] do if [ $lowercase = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' 'a-z') fi if [ $uppercase = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' 'A-Z') fi if [ $digits = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' '0-9') fi if [ $symbols = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' '!@#$%^&*()') fi done echo "Password $i: $password" # save the password to a file to a passwords.txt echo "Password $i: $password" >> passwords.txt done echo "Done! Passwords have been saved in a file passwords.txt" exit ;; 3) echo "Generating a password for a specific user" # ask user to enter the user name read -p "Enter the user name: " username # ask user to enter the password length read -p "Enter the password length: " length # ask user to add lowercase letters or uppercase letters or digits or symbols to the password read -p "Do you want to add lowercase letters? (y/n): " lowercase read -p "Do you want to add uppercase letters? (y/n): " uppercase read -p "Do you want to add digits? (y/n): " digits read -p "Do you want to add symbols? (y/n): " symbols # generate the password password="" while [ ${#password} -lt $length ] do if [ $lowercase = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' 'a-z') fi if [ $uppercase = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' 'A-Z') fi if [ $digits = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' '0-9') fi if [ $symbols = "y" ] then password=$password$(echo $RANDOM | tr '[0-9]' '!@#$%^&*()') fi done # display the password and user name and copy it to the clipboard echo "Your user name is: $username" echo "Your password is: $password" echo "$username:$password" | pbcopy echo "Password copied to clipboard" exit ;; 4) echo "Show password strength" # ask user to enter the password read -p "Enter the password: " password # calculate the strength of the password # a strong password should have at least one lowercase letter, one uppercase letter, one digit and one symbol # a weak password should have no lowercase letter, no uppercase letter, no digit and one symbol # a medium password should have at least one lowercase letter or one uppercase letter or one digit and one symbol # the passwords should be categorized as weak, strong, very strong, # calculate the number of lowercase letters lowercase_count=$(echo $password | grep -o -E '[a-z]' | wc -l) # calculate the number of uppercase letters uppercase_count=$(echo $password | grep -o -E '[A-Z]' | wc -l) # calculate the number of digits digits_count=$(echo $password | grep -o -E '[0-9]' | wc -l) # calculate the number of symbols symbols_count=$(echo $password | grep -o -E '[!@#$%^&*()]' | wc -l) # calculate the strength of the password if [ $lowercase_count -eq 0 ] && [ $uppercase_count -eq 0 ] && [ $digits_count -eq 0 ] && [ $symbols_count -eq 0 ] then strength="weak" elif [ $lowercase_count -gt 0 ] && [ $uppercase_count -gt 0 ] && [ $digits_count -gt 0 ] && [ $symbols_count -gt 0 ] then strength="strong" else strength="very strong" fi ;; Q) echo "Exiting..." exit ;; *) echo "Error: Invalid option" exit ;; esac
Write, Run & Share Bash code online using OneCompiler's Online Bash Shell for free. It's one of the robust, feature-rich Bash shell available over online and getting started with the OneCompiler's Bash Shell is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Bash
. OneCompiler also has reference scripts, where you can look for the sample scripts and start coding.
Bash (Bourne Again Shell) is a shell program written by Brian Fox and is an upgraded version of Bourne Shell program 'sh'.
name="Foo"
echo $name
echo "$name"
echo "${name}"
if [ conditional-expression ]
then
statements
fi
if [ conditional-expression ]
then
statements
else
statements
fi
if [ conditional-expression ]
then
statements
elif [ conditional-expression ]
then
statements
else
statements
fi