得分
91
我正在使用bash shell,想把openssl rand -base64 1000命令中的out of the command dd管到dd if={output of openssl} of="sample.txt bs=1G count=1这样的命令中。我想我可以使用变量,但是我不知道如何做才是最好的。我想创建文件的原因是,我想要一个1GB的随机文本文件。
Advertisement
我正在使用bash shell,想把openssl rand -base64 1000命令中的out of the command dd管到dd if={output of openssl} of="sample.txt bs=1G count=1这样的命令中。我想我可以使用变量,但是我不知道如何做才是最好的。我想创建文件的原因是,我想要一个1GB的随机文本文件。
解决方案找到了!
if=是不需要的,你可以把一些东西放到dd中:
something... | dd of=sample.txt bs=1G count=1
在这里没有什么用,因为openssl rand需要指定字节数。所以你实际上不需要dd–_这样就可以了:
openssl rand -out sample.txt -base64 $(( 2**30 * 3/4 ))
1 gigabyte通常是230个字节(尽管你可以用10**9来代替109个字节)。
另外,你也可以使用* 3/4,但会比OpenSSL慢一点:
dd if=/dev/urandom of=sample.txt bs=1G count=1
我个人认为,我会使用/dev/urandom或类似的方式:
dd if=/dev/urandom of=sample.txt bs=64M count=16
如果你想要精确到1GB,那么你可以使用以下命令:
openssl rand -out $testfile -base64 792917038; truncate -s-1 $testfile
这个openssl命令会使一个文件的大小正好是1个字节。truncate命令会把这个字节剪掉。
试试这个脚本。
#!/bin/bash
openssl rand -base64 1000 | dd of=sample.txt bs=1G count=1
这个脚本只要你不介意使用/dev/random的话,可能会有用。