批量生成随机字符文件名案例。
使用for循环在/oldboy目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串oldboy,名称示例如下:
[root@oldgirl C19]# ls /oldboy
apquvdpqbk_oldboy.html mpyogpsmwj_oldboy.html txynzwofgg_oldboy.html
bmqiwhfpgv_oldboy.html mtrzobsprf_oldboy.html vjxmlflawa_oldboy.html
jhjdcjnjxc_oldboy.html qeztkkmewn_oldboy.html
jpvirsnjld_oldboy.html ruscyxwxai_oldboy.html
方法1:1
#!/bin/bash
[ ! -d /oldboy ] && mkdir /oldboy -p
for i in {01..10}
do
pass=`uuidgen|cut -c 1-10|tr [0-9-] [a-z]`
touch /oldboy/${pass}_oldboy.html
done
方法2:1
#!/bin/bash
dir=/oldboy
[ -d $dir ] || mkdir -p $dir
cd $dir && \
for i in {01..10}
do
shu=$(uuidgen|tr '0-9-' 'a-z'|cut -c 1-10)
touch ${shu}_oldboy.html
done
方法3:
#!/bin/bash
. /etc/init.d/functions
DIR=/oldboy4
[ -d $DIR ]||mkdir /oldboy4 -p
cd $DIR &>/dev/null||{
echo "cd $DIR fail"
exit 2
}
cd $DIR &&{
for i in {1..10}
do
touch $(echo $RANDOM|md5sum|tr [0-9] [a-z]|cut -c 1-10
)_oldboy.html
action "file create success!" /bin/true
done
exit 0
}
echo "file create fail!"
方法4:1
#!/bin/bash
. /etc/init.d/functions
[ -d /oldboy ]|| mkdir -p /oldboy
cd /oldboy
for i in `seq 10`
do
touch `echo $RANDOM|md5sum|cut -c 1-10|tr "[0-9]" "[a-z]"`_oldboy.html
done