Sheldon Xu

Shell: 生成随机字符串

生成一个二十位的随机字符串:

#!/bin/bash

randstr() {
  index=0
  str=""
  for i in {a..z}; do arr[index]=$i; index=`expr ${index} + 1`; done
  for i in {A..Z}; do arr[index]=$i; index=`expr ${index} + 1`; done
  for i in {0..9}; do arr[index]=$i; index=`expr ${index} + 1`; done
  for i in {1..20}; do str="$str${arr[$RANDOM%$index]}"; done
  echo $str
}

echo `randstr`

  

posted on 2012-11-24 10:48  Sheldon Xu  阅读(5889)  评论(0编辑  收藏  举报

导航