免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8122 | 回复: 0
打印 上一主题 下一主题

[请教]如何随机的生成一个字符串?[请教]  关闭 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2004-02-17 14:36 |显示全部楼层

[请教]如何随机的生成一个字符串?[请教]

本帖最后由 rdcwayx 于 2014-09-04 14:54 编辑
  1. #!/bin/bash
  2. # May need to be invoked with #!/bin/bash2 on older machines.
  3. #
  4. # Random password generator for bash 2.x by Antek Sawicki <tenox@tenox.tc>,
  5. # who generously gave permission to the document author to use it here.
  6. #
  7. # ==> Comments added by document author ==>
  8. MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  9. LENGTH="8"
  10. # ==> May change 'LENGTH' for longer password, of course.
  11. while [ "${n:=1}" −le "$LENGTH" ]
  12. # ==> Recall that := is "default substitution" operator.
  13. # ==> So, if 'n' has not been initialized, set it to 1.
  14. do
  15. PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
  16. # ==> Very clever, but tricky.
  17. # ==> Starting from the innermost nesting...
  18. # ==> ${#MATRIX} returns length of array MATRIX.
  19. # ==> $RANDOM%${#MATRIX} returns random number between 1
  20. # ==> and length of MATRIX − 1.
  21. # ==> ${MATRIX:$(($RANDOM%${#MATRIX})):1}
  22. # ==> returns expansion of MATRIX at random position, by length 1.
  23. # ==> See {var:pos:len} parameter substitution in Section 3.3.1
  24. # ==> and following examples.
  25. # ==> PASS=... simply pastes this result onto previous PASS (concatenation).
  26. # ==> To visualize this more clearly, uncomment the following line
  27. # ==> echo "$PASS"
  28. # ==> to see PASS being built up,
  29. # ==> one character at a time, each iteration of the loop.
  30. let n+=1
  31. # ==> Increment 'n' for next pass.
  32. done
  33. echo "$PASS" # ==> Or, redirect to file, as desired.
  34. exit 0
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP