在 Bash 中按照特定模式生成随机字符串的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7032547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:34:13  来源:igfitidea点击:

Best way to generate a random string following a specific pattern in Bash

linuxstringbashrandom

提问by Gerry

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

我想按照这个模式生成许多六个字符的字符串:

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

[辅音][元音][辅音][辅音][元音][辅音]

e.g.

例如

haplop
github
qursog

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.

我已经研究了这样做的各种方法,但到目前为止我所知道的没有什么是优雅的。我的想法主要围绕着一次生成一个角色,但即便如此,由于我缺乏 bash 脚本知识和晦涩的 Linux 命令,我的想法并不是那么好。

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.

理想情况下,我正在寻找一个 Linux 命令,它随机生成一个字符串,但允许我指定上面显示的模式(如果存在的话)。或者,如果您知道在 bash 中执行此操作的简单方法,那也很棒。

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).

编辑:顺便说一句,我会在选择接受的答案前 24 小时给出这个答案,这样我就有更多机会选择最佳答案,而不仅仅是第一个(尽管第一个答案非常好)。

回答by Karoly Horvath

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`

回答by user unknown

#!/bin/bash
#
#
cprint () {
    typ=
    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=
    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.

这个解决方案很容易改变以打印不同的序列,很容易重新定义,如果你不同意 y,需要 ??ü 或 UPPERCASE 等等。

Experimental result:

实验结果:

gohhec
voteup
Wuwjut
Utpycoq
Va6a6

man bash says about RANDOM:

man bash 说到 RANDOM:

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

每次引用此参数时,0 到 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. :)

因此,如果取模RANDOM % X,并且 X 不是 2 的幂,则较低的余数将比较高的一次有更好的机会。对你来说,它应该不重要,只要你不把你的角色组带到极端,或者在高安全区域使用它。:)

回答by Gerry

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":

为了后代,我将包括我最终得到的代码。它主要使用 Karoly Horvath 的代码,但我也添加了一些来自“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..}); do
        echo `c;v;c;c;v;c;`
    done
}

genPatternStrings ;

回答by Srihari Konakanchi

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