Password Generator
re-learning bash from being at uni, and making a password generator. I want to be able to output the results to a text file can anyone help? my code so far is:
#!/bin/bash
#Password Generator
#Account Name
echo "What account is this password for"
read account
#username
echo "Please give your username"
read username
#ask for password length
echo "please provide a password length"
read length
echo "Your password is"
openssl rand -base64 $lengthI would like to be able to output the results from account, username, and what the password is to the text file Thanks in advance for any help, and i hope this query makes sense!
2 Answers
You can redirect any output with echo like: cmd > filename or in your case:
openssl rand -base64 $length > passwords.txt 2 There are several ways to do this, but ultimately only 2 approaches: pipe to a file, or pipe to a program that writes to a file.
I've created some modified versions of your scripts below, demonstrating some methods.
Output To A New File (Gedit):
#!/bin/bash
#Password Generator A 1.1
#A random password generator written for Bash.
#Dependencies: Gedit, OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
##Generate Password
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal
echo "$output"
###Output to new file in Gedit
echo "$output" | gedit -
exit 0Output To A New File (Nano):
#!/bin/bash
#Password Generator B 1.1
#A random password generator written for Bash.
#Dependencies: Nano, OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
##Generate Password
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal
echo "$output"
###Output to new file in Nano
echo "$output" | nano -
exit 0Output To A Text File Located At "~/output":
#!/bin/bash
#Password Generator C 1.1
#A random password generator written for Bash.
#Dependencies: OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
##Generate Password
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal
echo "$output"
###Output to file
echo "$output" > "$HOME/output"
exit 0Output To A Text File Located At "~/output" (Using Tee):
#!/bin/bash
#Password Generator D 1.1
#A random password generator written for Bash.
#Dependencies: Tee, OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
##Generate Password
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal & file
echo "$output" | tee "$HOME/output"
exit 0Output To A Text File (User chooses):
#!/bin/bash
#Password Generator E 1.1
#A random password generator written for Bash.
#Dependencies: OpenSSL
##Request Inputs
###Account Name
echo -n "Account Name:"
read account
###Username
echo -n "Username:"
read username
###Password Length
echo -n "Password length:"
read length
###Save Location
echo -n "Save to:"
read savelocation
##Generate Password
password=$(openssl rand -base64 $length)
##Generate Result
###Merge variables
output=$(echo -e "Account Name:${account}\nUsername:${username}\nPassword:${password}")
##Output result
###Output to terminal
echo "$output"
###Output to file
echo "$output" > "$savelocation"
exit 0 1