采用随机生成在bash一个随机字符串 [英] using random to generate a random string in bash
本文介绍了采用随机生成在bash一个随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用bash函数RANDOM创建一个由8个字符从包含整数和字母数字变量的随机字符串(例如:VAR =abcd1234ABCD)
I am trying to use the bash function RANDOM to create a random string that consists of 8 character from a variable that contains integer and alphanumeric digits (eg: var="abcd1234ABCD")
感谢您。
推荐答案
使用参数扩展。 $ {#字符}
是可能的字符数,%
是模运算符。 $ {字符:偏差:长度}
选择在位置的字符(S)偏移
,即0 - 长($字符)在我们的例子。
Use parameter expansion. ${#chars}
is the number of possible characters, %
is the modulo operator. ${chars:offset:length}
selects the character(s) at position offset
, i.e. 0 - length($chars) in our case.
chars=abcd1234ABCD
for i in {1..8} ; do
echo -n ${chars:RANDOM%${#chars}:1}
done
echo
这篇关于采用随机生成在bash一个随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文