Bash Loop for password gen

Hey guys, I'm trying to make a bash script to do password generation.
The script takes 2 arguments, number of characters and number of passes to generate, but I can't get the loop to work properly.

#!/bin/bash
echo
echo
echo -ne "Password length:"
read pwd_length
echo
echo -ne "Number of passes to generate:"
read pwd_number

char=(0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! @ \# $ % ^ \& \+ - \) \( = )

counter=0
while [ $counter -le $pwd_number ]
do
max=${#char
[*]}
for i in `seq 1 $pwd_length`
do
let rand=${RANDOM}%${max}
counter=$(( $counter + 1 ))
out="${out}${char[$rand]}"
done

echo
echo "$out"
done

Unless you just have to create a new script, check out this bash function:
Bash Random Password Generator | LegRoom.net

Yeah I saw some of the other ones around, I just want to know what is wrong with my script for the future

What is wrong with your script? What is the error?

--ahamed

When it runs it only ever spits out a max of 2 results, the second if always 50% of the first and then another x number of chars, see below:

Password length:2

Number of passes to generate:3

9a

9aCk

Try these changes...

...
counter=0
max=${#char[*]}

while [ $counter -lt $pwd_number ]
do
  out=""
  for i in `seq 1 $pwd_length`
  do
    let rand=${RANDOM}%${max}
    out="${out}${char[$rand]}"
  done

  counter=$(( $counter + 1 ))
  echo
  echo "$out"
done

--ahamed

1 Like

Legend, that's fixed it, thanks bro!