在Azure Cloud Shell Bash中生成随机字符串时出现问题

xvw2m8pv  于 2022-11-17  发布在  Shell
关注(0)|答案(2)|浏览(116)

我正在尝试在Bash中生成一个特定长度的随机字符串。这在我所能测试的其他Bash shell中都是有效的(Ubuntu 18.04 LTS和Debian 10)

$ cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 10 | head -n 1
n8a4wxvb01
$

cat /dev/urandom-无限字节流
tr -dc ‘a-z0-9’-仅返回小写字符和数字
fold -w 10-在10个字符处中断
head -n 1-返回流的第一行
在Azure Cloud Shell Bash中,这个函数挂起。它返回随机字符串,但从不退出。对head -n 1的调用在打印值后从不退出。
我有一个解决方案,但似乎是反的

head -n 10 /dev/urandom | tr -dc 'a-z0-9' | fold -w 10 | head -n 1
z9ju0rcb

z9ju0rcb1#

该命令在个人Linux操作系统和Azure Cloud Shell Bash中都能正常工作。
碧云贝捶:

个人Linux操作系统:

我认为两者之间不会有什么区别,Azure云壳只是一个运行在Ubuntu 16.04 LTS上的VM,看看the features of the Azure Cloud Shell

更新日期:

你可以看一下issue,答案可能显示了命令在Azure Cloud Shell中不起作用的原因。而且据我所知,它不是一个高性能的系统。否则,使用cat来获得随机字符串不是一个好主意。我建议使用head

b5buobof

b5buobof2#

我更喜欢使用openssl来创建随机字符串(十六进制或base64):

openssl rand -hex 10

openssl rand -base64 10

https://www.openssl.org/docs/man1.0.2/man1/openssl-rand.html

相关问题