随机生成一个文件怎样才能充满在shell脚本随机数字或字母随机文件?我也想指定文件的大小。使用shell脚本
回答
使用dd
命令来读取从/ dev /随机数据。
dd if=/dev/random of=random.dat bs=1000000 count=5000
这将读取5000个1MB的随机数据块,即整个5千兆字节的随机数据!
实验用的块大小参数,以获得最佳性能。
随机变量每次都会给你一个不同的号码:
echo $RANDOM
head -c 10 /dev/random > rand.txt
变化10至什么。阅读“man随机”以了解/ dev/random和/ dev/urandom之间的区别。
或者,只是BASE64字符
head -c 10 /dev/random | base64 | head -c 10 > rand.txt
的基于64可能包括一些字符你不感兴趣,但没有时间去想出一个更好的单衬字符转换器... (也我们从/ dev /随机对不起,熵池以字节太多!)
哎呀,错过了字符和数字部分,我猜你的意思是字母数字字符......需要修改。 – 2010-04-06 18:01:57
拯救熵:在绿色生活中的最新趋势。 :) – 2010-04-06 18:20:20
一个良好的开端是:
http://linuxgazette.net/153/pfeiffer.html
#!/bin/bash
# Created by Ben Okopnik on Wed Jul 16 18:04:33 EDT 2008
######## User settings ############
MAXDIRS=5
MAXDEPTH=2
MAXFILES=10
MAXSIZE=1000
######## End of user settings ############
# How deep in the file system are we now?
TOP=`pwd|tr -cd '/'|wc -c`
populate() {
cd $1
curdir=$PWD
files=$(($RANDOM*$MAXFILES/32767))
for n in `seq $files`
do
f=`mktemp XXXXXX`
size=$(($RANDOM*$MAXSIZE/32767))
head -c $size /dev/urandom > $f
done
depth=`pwd|tr -cd '/'|wc -c`
if [ $(($depth-$TOP)) -ge $MAXDEPTH ]
then
return
fi
unset dirlist
dirs=$(($RANDOM*$MAXDIRS/32767))
for n in `seq $dirs`
do
d=`mktemp -d XXXXXX`
dirlist="$dirlist${dirlist:+ }$PWD/$d"
done
for dir in $dirlist
do
populate "$dir"
done
}
populate $PWD
另存为 “script.sh”,如运行SIZE ./script.sh。 printf代码从http://mywiki.wooledge.org/BashFAQ/071中解除。当然,你可以初始化mychars阵列蛮力,mychars =( “0” 和 “1” ...... “A” ...... “Z”, “A” ... “Z”),但不会有什么乐趣,是吗?
#!/bin/bash
declare -a mychars
for ((I=0; I<62; I++)); do
if [ $I -lt 10 ]; then
mychars[I]=$I
elif [ $I -lt 36 ]; then
D=$((I+55))
mychars[I]=$(printf \\$(($D/64*100+$D%64/8*10+$D%8)))
else
D=$((I+61))
mychars[I]=$(printf \\$(($D/64*100+$D%64/8*10+$D%8)))
fi
done
for ((I=$1; I>0; I--)); do
echo -n ${mychars[$((RANDOM%62))]}
done
echo
/dev/random&base64方法也很好,而不是通过“tr -d -c [:alnum:]”管道穿过base64,管道,然后你只需要计算出来的好字符,直到你'重做。 – nortally 2011-07-28 16:29:53
大小创建100个随机命名的50MB的文件中的每个:
for i in `seq 1 100`; do echo $i; dd if=/dev/urandom bs=1024 count=50000 > `echo $RANDOM`; done
最好使用mktemp来创建随机文件。 for i in seq 1 100; do myfile ='mktemp --tmpdir = .' dd if =/dev/urandom bs = 1024 count = 50000> $ myfile done – 2012-10-11 05:51:44
- 1. 使用shell脚本
- 2. 使用shell脚本
- 3. 使用shell脚本
- 4. 使用shell脚本
- 5. 使用shell脚本
- 6. 使用shell脚本
- 7. 使用shell脚本
- 8. 使用shell脚本
- 9. 使用shell脚本
- 10. 使用shell脚本
- 11. shell脚本Groovy脚本使用GANT
- 12. 如何使用shell脚本
- 13. 使用sed的shell脚本
- 14. 如何使用shell脚本
- 15. 使用shell脚本 - 星号
- 16. 如何使用shell脚本
- 17. 比较使用shell脚本
- 18. 我有使用shell脚本
- 19. 查找使用shell脚本
- 20. 阅读使用shell脚本
- 21. 批量使用shell脚本
- 22. 如何使用shell脚本
- 23. 如何使用shell脚本
- 24. 使用或shell脚本
- 25. Shell脚本使用DBLINK
- 26. SH - 使用shell脚本
- 27. 搜索使用shell脚本
- 28. include_once使用shell脚本
- 29. 查找 - 使用shell脚本
- 30. 如何使用shell脚本
哪些字符被允许在输出文件?任何随机字节或只是ascii字母数字字节? – 2010-04-06 18:23:08