A random string generator in a bash script isn't respecting the number of given characters

前端 未结 4 857
时光说笑
时光说笑 2020-12-22 01:08

I am trying to build a random character generator in a bash script on osx 10.8.5 . The goal is to generate random character strings for a script generating salts for the wor

4条回答
  •  爱一瞬间的悲伤
    2020-12-22 01:34

    I see two problems in your script.

    I'm pretty sure

    take=$(($RANDOM % 88));
    

    should be

    take=$(($RANDOM % 87));
    

    Otherwise, it appears you're going past the end of your input stream.

    The other problem is this char:

    Bash is seeing that as two characters (wide char?). I'd delete it from your possibilities.

    Of course, that would mean the above line would be:

    take=$(($RANDOM % 86));
    

    Doing those two things, in fact, works for me.

    Edited:

    @that other guy has a better answer. Adding the space rather than reducing the modulo will ensure that you get every character

提交回复
热议问题