Shell-shell 生成随机字母数字组合的问题
之前在社区里看到了一个类似的帖子这个脚本是如何产生随机密码的?
自己想到了一个其他的实现方式:
head -c 500 /dev/urandom | sed 's/[^a-zA-Z0-9]//g' | head -c 16; echo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$ tr -dc '0-9a-zA-Z' < /dev/urandom | fold -w 16 | head -10
3zf2TnCa4AyoxkBV
wGWgUrLFSHh8y4gZ
DVCRBHvCVh194FO3
yJ3xToa1LptXVnNG
SYQ8vR1PRUlxeSSr
upCHmUAuZMyaAiQH
QvSVaTEhaxdpTjLH
bt9W3Gw1lIqZRKZA
nCcF3X4O6jJwX6Yx
UvNjSFGbesC5IlCY
评论写不下,直接贴出来,供浏览的人直接学习
Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive, using the locale’s collating sequence and character set.For example, in the default C locale, [a-d] is equivalent to [abcd].Many locales sort characters in dictionary order, and in these locales [a-d] is typically not equivalent to [abcd]; it might be equivalent to [aBbCcDd],for example.To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value C.
需要在调用
sed
前传入环境变量LANG=C
,否则[a-zA-Z0-9]
表示的并不仅仅是字母数字。head -c 500 /dev/urandom |
LANG=C sed 's/[^a-zA-Z0-9]//g' |
head -c 16