7

I'm wanting to generate many six character strings all following this pattern:

[consonant][vowel][consonant][consonant][vowel][consonant]

e.g.

haplop
github
qursog

I've looked at various ways of doing this, but nothing I have though of so far is elegant. My thoughts mostly revolved around generating one character at a time but even then my ideas weren't really all that good due to my lack of bash scripting knowledge and obscure Linux commands.

Ideally I'm looking for a Linux command that randomly generates a string but allows me to specify the pattern shown above (if one even exists). Alternatively if you know of a simple way of doing this in bash that would also be great.

Thanks in advance

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

4
  • p.s. I'm not using this to generate passwords, they would be far too insecure :) Commented Aug 11, 2011 at 20:42
  • 1
    Why? If you extend the length to 12 characters? For a related discussion see here: security.stackexchange.com/questions/6095/… Commented Aug 11, 2011 at 23:25
  • Well I meant specifically six characters. Although personally I'm quite paranoid. One of the passwords I've memorized is made up of upper case, lower case, numbers, special characters and is 20 characters long. Also with the existence of encrypted password managers there is no good reason to have weak passwords. But yeah, I was taking specifically about the six character pattern I gave. Commented Aug 12, 2011 at 8:20
  • @user_unknown: also +1 for the link :) I didn't realise there was a Stack Exchange dedicated to security. Commented Aug 12, 2011 at 8:23

4 Answers 4

16

Here is 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 into a small function:

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

And then you have a nice pattern to generate the string:

s=`consonant; vowel; consonant; vowel; consonant; vowel`
Sign up to request clarification or add additional context in comments.

4 Comments

The functions are a flash of genius. I was going to to what you did, but put it in a single program.
it's just a by-product of the process, I didn't intend it to be that nice :)
Well done, and thank you. This was an awesome answer. It also surprisingly performs about twice as fast as using a switch statement, on my computer.
The Bash feature behin ${s:$p:1} is "Substring Expansion", explained in the Bash manual, chapter 3.5.3 Shell Parameter Expansion. Just a note, as I was wondering how that part works, and googling for brackets is kind of tricky.
2

#!/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 solution is easily changed to print a different sequence, is easily redefined, if you don't agree about the y, need äöü or UPPERCASE and so on.

Experimental result:

gohhec
voteup
Wuwjut
Utpycoq
Va6a6

man bash says about RANDOM:

Each time this parameter is referenced, a random integer between 0 and 32767 ...

So if take the modulo RANDOM % X, and X is not a power of 2, the lower remainders will have better chances than higher once. It shouldn't be important in your case, as long as you don't go to the extereme with your character groups, or use it in high security areas. :)

1 Comment

Thanks for your answer. I ended up using the other answer, but I also gave yours a +1.
1

For posterity, I'll include the code I ended up with. It mostly uses Karoly Horvath's 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;

Comments

1

Though the following command doesn't 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 'A-Z' 'a-z'       # lowercase
done | less

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.