Mud
Mud

Reputation: 84

Generate two random symbols for mysql password in bash

Hi I have a bash script I wrote for my plesk installation.

It triggers when a new subscription is created and does the following:

One problem though is sometimes I generate two symbols that seem to invalidate my password string. I think it's because some symbols need escaping in bash and it's not happening. Is there a way I can randomly grab two symbols from an array of "allowed" symbols?

Here is my genPassword() function

genpasswd() {
local pass=`cat /dev/urandom | tr -cd "[:punct:]" | head -c 2`
echo "$pass"
echo "INFO: genpasswd() ran successfully" >> /usr/run.log
}

Upvotes: 0

Views: 580

Answers (1)

Sylvain Leroux
Sylvain Leroux

Reputation: 52030

Using bash arrays and the "magic" variable RANDOM might help:

bash$ symbols=(":" "+" ";" ".")
bash$ echo ${symbols[ RANDOM % ${#symbols[@]} ]}
;

bash$ echo ${symbols[ RANDOM % ${#symbols[@]} ]}
+

If you need to generate a pair (for example):

bash$ s1=${symbols[ RANDOM % ${#symbols[@]} ]}
bash$ s2=${symbols[ RANDOM % ${#symbols[@]} ]}
bash$ echo "${s1}${s2}"
;:

Upvotes: 1

Related Questions