生成10個隨機文件
要求:生成10個文件且文件名為10個字符長度。
Linux中產生隨機數的六種方法:
方法一:通過系統環境變量($RANDOM)實現,實例:
[[email protected] scripts]# echo $RANDOM
20305
[[email protected] scripts]# echo $RANDOM
1533
方法二:通過OpenSSL產生隨機數,實例如下:
[[email protected] scripts]# openssl rand -base64 20
xq3TTgKFEbVM91vOxaVlFcm9/5k=
[[email protected]
85jksJtRW+4=
方法三:通過時間(date)獲取隨機數,實例如下:
[[email protected] scripts]# date +%s%N
1500541761385704079
[[email protected] scripts]# date +%s%N
1500541763206051706
方法四:通過/dev/urandom配合chksum生成隨機數,實例如下:
[[email protected] scripts]# head /dev/urandom |cksum
3528005773 4145
[[email protected] scripts]# head /dev/urandom |cksum
397462229 2644
註意:後面必須帶cksum,否則是亂碼
方法五:通過UUID生成隨機數,實例如下:
[[email protected] scripts]# cat /proc/sys/kernel/random/uuid
be6f6907-ae36-445a-9711-f3bc7ee87cf8
[[email protected] scripts]# cat /proc/sys/kernel/random/uuid
bf3ca383-db40-41f0-9d61-92e7763d71ce
具體實現代碼如下:
#!/bin/bash
#Author:dadong
#Blog:http://dadonggg.blog.51cto.com
#Time:2017-07-06 19:25:10
#Name:cd.sh
#Version:V1.0
#Description:This is a test script.
path=/data
[ -d $path ]|| mkdir $path && cd $path
for i in {1..10}
do
name=`cat /proc/sys/kernel/random/uuid|cut -c 1-10`
touch ${name}.oldboy.html
done
本文出自 “寫個博客騙錢” 博客,請務必保留此出處http://dadonggg.blog.51cto.com/12672150/1949908
生成10個隨機文件