The best way to generate a random string by following a specific pattern in Bash

I want to generate many sixteen characters following this pattern:

[consonant] [vowels] [consonant] [consonant] [vowels] [consonant]

eg.

haplop
Github
qursog

I looked at various ways to do this, but none of what I still have is elegant. My thoughts mostly revolved around creating one character at a time, but even then my ideas were not really that good due to a lack of knowledge of bash scripts and obscure Linux commands.

Ideally, I am looking for a Linux command that randomly generates a string, but allows me to specify the pattern shown above (if it exists). Alternatively, if you know an easy way to do this in bash, that would also be great.

Thanks in advance

Edit: BTW, I will give this 24 hours before choosing the accepted answer, so that I have a better chance of choosing the best answer, and not just the first (although the first answer was pretty good).

+6
source share
4 answers

Here's how you could generate a vowel:

s=aeiou p=$(( $RANDOM % 5)) c=${s:$p:1} 

Use the same method for consonants.

You can wrap this in a small function:

 function vowel() { s=aeoiu p=$(( $RANDOM % 5)) echo -n ${s:$p:1} } 

And then you have a nice template for creating a string:

 s=`consonant; vowel; consonant; vowel; consonant; vowel` 
+13
source

 #!/bin/bash # # cprint () { typ=$1 case $typ in v) alpha="aeiouy" ;; c) alpha="bcdfghjklmnpqrstvwxz" ;; V) alpha="AEIOUY" ;; C) alpha="BCDFGHJKLMNPQRSTVWXZ" ;; n) alpha="0123456789" ;; *) echo "** Undefined **" ; exit 1 ;; esac len=${#alpha} r=$((RANDOM%len)) echo -en ${alpha:r:1} } rprint () { code=$1 for i in $(seq 1 ${#code}) do c=${code:i-1:1} cprint $c done echo } rprint "cvccvc" rprint "cvcvvc" rprint "Cvccvc" rprint "Vccvcvc" rprint "Cvnvn" 

This decision is easy to change to print a different sequence, easily redefined if you do not agree with y, need Àâü or UPPERCASE, etc.

The experimental result:

 gohhec voteup Wuwjut Utpycoq Va6a6 

man bash talks about RANDOM:

Every time this parameter is referenced, a random integer from 0 to 32767 ...

So, if you take modulo RANDOM % X , and X is not a power of 2, the lower balances will have better chances than higher once. This should not be important in your case, if you do not go to the extrem with your groups of characters or do not use it in high security zones. :)

+2
source

For posterity, I will include the code in which I ended up. It mainly uses Karoly Horvath code, but I also added some ideas from "user unknown":

 #!/bin/bash function v() { s=aeoiuy p=$((RANDOM % ${#s})) echo -n ${s:$p:1} } function c() { s=bcdfghjklmnpqrstvwxz p=$((RANDOM % ${#s})) echo -n ${s:$p:1} } function genPatternStrings() { for i in $(eval echo {0..$1}); do echo `c;v;c;c;v;c;` done } genPatternStrings $1; 
+1
source

Although the following command does not answer the original question, it may be useful in other cases to generate any random string:

 for i in {1..15}; do # 15 random strings dd status=noxfer if=/dev/random bs=1 count=200 2>/dev/null | tr -dc 'a-z0-9A-Z' | # alphanumerics only cut -b1-10 | # length 10 tr 'AZ' 'az' # lowercase done | less 
+1
source

Source: https://habr.com/ru/post/894878/


All Articles