1

I am creating a password generator however I am not get the passwords to append properly.

Here is my script

i="0"
while [ $i -lt 5 ]
do
    echo -n '#' >> passwords.txt && openssl rand -hex 4 >> passwords.txt && echo -n '/' >> passwords.txt && echo -n 'X' >> passwords.txt
    i=$[$i+1]
done

Output

#b887e0d0
/X#7093289e
/X#2210cfcd
/X#fd175e1f
/X#0c18fc9e
/X

Expected Output

#b887e0d0/Z
#7093289e/Z
#2210cfcd/Z
#fd175e1f/Z
#0c18fc9e/Z

How can I make it so that it doesn't skip that first line and also so that it runs each command in that order and will write to the passwords txt file as the expected output.

Thanks.

  • Thanks David King, dhag and lese for all these answers, really appreciate it :) – Ryan Hawdon Dec 17 '15 at 14:07
6

This should do:

for i in {1..5}; do
    printf '#%s/Z\n' "$(openssl rand -hex 4)"
done >passwords.txt

I replaced the multiple calls to echo with a single call to printf. Having the call to openssl wrapped inside a command substitution has the side effect of making the line ending disappear, and that newline character was the cause of the badly-placed line breaks visible in your example.

4

The problem is that openssl is being 'helpful' and placing a newline at the end of the random string. You can remove that by piping it through tr

openssl rand -hex 4 | tr -d '\n'
  • Thank you so much :) I have been spending a while trying fix this. Thanks. – Ryan Hawdon Dec 17 '15 at 14:05

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.