1

I am creating a Password Generator using 4 Arrays (Lower Case, Upper Case, Symbol and Number) with the RANDOM function. Basically I will generate a random number and then apply it to each of those arrays.

Here is my script.

LOWER[0]="a"
LOWER[1]="b"
LOWER[2]="c"
LOWER[3]="d"
LOWER[4]="e"
LOWER[5]="f"
LOWER[6]="g"
LOWER[7]="h"
LOWER[8]="i"
LOWER[9]="j"
LOWER[10]="k"
LOWER[11]="l"
LOWER[12]="m"
LOWER[13]="n"
LOWER[14]="o"
LOWER[15]="p"
LOWER[16]="q"
LOWER[17]="r"
LOWER[18]="s"
LOWER[19]="t"
LOWER[20]="u"
LOWER[21]="v"
LOWER[22]="w"
LOWER[23]="x"
LOWER[24]="y"
LOWER[25]="z"

UPPER[0]="A"
UPPER[1]="B"
UPPER[2]="C"
UPPER[3]="D"
UPPER[4]="E"
UPPER[5]="F"
UPPER[6]="G"
UPPER[7]="H"
UPPER[8]="I"
UPPER[9]="J"
UPPER[10]="K"
UPPER[11]="L"
UPPER[12]="M"
UPPER[13]="N"
UPPER[14]="O"
UPPER[15]="P"
UPPER[16]="Q"
UPPER[17]="R"
UPPER[18]="S"
UPPER[19]="T"
UPPER[20]="U"
UPPER[21]="V"
UPPER[22]="W"
UPPER[23]="X"
UPPER[24]="Y"
UPPER[25]="Z"

SYMBOL[0]="!"
SYMBOL[1]="@"
SYMBOL[2]="#"
SYMBOL[3]="$"
SYMBOL[4]="%"
SYMBOL[5]="^"
SYMBOL[6]="&"
SYMBOL[7]="*"
SYMBOL[8]="("
SYMBOL[9]=")"

NUMBER[0]="1"
NUMBER[1]="2"
NUMBER[2]="3"
NUMBER[3]="4"
NUMBER[4]="5"
NUMBER[5]="6"
NUMBER[6]="7"
NUMBER[7]="8"
NUMBER[8]="9"
NUMBER[9]="0"

for i in {1..5}; do
  for j in {1..3}; do
    let RANDLOWER=$RANDOM%26
    let RANDUPPER=$RANDOM%26   
    let RANDSYMBOL=$RANDOM%10
    let RANDNUMBER=$RANDOM%10

    echo -n ${LOWER[$RANDLOWER]}
    echo -n ${UPPER[$RANDUPPER]}
    echo -n ${SYMBOL[$RANDSYMBOL]}
    echo -n ${NUMBER[$RANDNUMBER]}

  done >> passwords.txt
echo >> passwords.txt
done

However, when it generates and outputs to the passwords.txt it will show a list of all directories of where the script is running.

For example, I am running this script in the ~ directory and it will display all the directories.

passwords.txt output

bRApplications Desktop Documents Downloads Library Movies Music Pictures Public passwords.txt9vN$3qC(1
sB$1tH#3uV&6
tK#3hK#2bE!
sM^4cP%5sC^3
sB&6lG)6lJ!6

Expected output

sC@1zH&6wO$2
sQ@0vC@2hT%1
rL&7kY%5gY#1
wI#9hC&3oA&4
cZ%6uA!6xM(3

What am I missing in this script, why is it giving me this output?

2
  • Not an answer to the question but - why such a complicated method? What's wrong with tr -cd '[:graph:]' < /dev/urandom | fold -w12 | head -n5 to generate random all chars passwords? If you want a more specific char set, instead of [:graph:] you might use !@#$%^&*()A-Z-a-z-0-9 as your tr set. – Dani_l Jan 8 '16 at 14:24
  • Hi @Pandya, it is strange that it will generate some of the passwords fine, however it will then display the directories of where the script is running. – Ryan Hawdon Jan 8 '16 at 14:37
1

You can get rid of your problem by making the following edit to your script:

echo -n ${LOWER[$RANDLOWER]}${UPPER[$RANDUPPER]}${SYMBOL[$RANDSYMBOL]}${NUMBER[$RANDNUMBER]}

done >> passwords.txt
echo >> passwords.txt
done

For me, it is giving sample output as expected:

$ cat passwords.txt 
dI)9oF*5mG(1
pO&9rL%7cO^9
vB#8zF^4uN)3
hW(5dA%0sT^4
iQ*2hG*2gV!7
3
  • Hi @Pandya, it is giving me the following output. These are on separate lines sG&6 kY#2 fP@6 lJ)5 mA*6 rR@2 gV)3 hN%2 jW@6 tZ#3 rV!3 xZ^7 wJ)9 uU)0 nK$6 – Ryan Hawdon Jan 8 '16 at 14:28
  • @RyanHawdon I've revised the answer, now is it working? – Pandya Jan 8 '16 at 14:42
  • 1
    Works perfectly! Thank you very much!! I have marked your post as the answer, however I can't up vote it since I don't have 15 reputation, sorry. Thanks again :) – Ryan Hawdon Jan 8 '16 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.