需求描述
linux下随机产生一个指定长度的小写字母和数字组合成的字符串
脚本内容
vim random.sh
测试
chmod +x random.sh
./random.sh
输出 idr
./random.sh 4
输出 feda
linux下随机产生一个指定长度的小写字母和数字组合成的字符串
脚本内容
vim random.sh
#!/bin/sh
getrandom(){
if [ $# -gt 0 ]; then
size=$1
else
size=3
fi
cat /dev/urandom | sed 's/[^a-z0-9]//g' | strings -n $size | head -n 1
}
getrandom $1
exit 0
测试
chmod +x random.sh
./random.sh
输出 idr
./random.sh 4
输出 feda