一个有趣的Shell随机数

需求描述
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

猜你喜欢

转载自i-david.iteye.com/blog/1333540