shell生成8位的随机字符串
#!/bin/bash
function rand_str()
{
chars=012345689abcdefghiklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ
key=""
for i in {1..8} ; do
key=$key"${chars:RANDOM%${#chars}:1}"
done
echo $key
}
rand_str
${#chars} : the number of possible characters
${chars:offset:length}: got characters in chars that shifted at offset, including its next length characters. For example ${chars:3:2} is 34
RANDOM%${#chars} narrows the offset to 0-length of the chars, and :1 tells to get the next 1 character.
这篇博客介绍了如何使用shell脚本创建一个8位的随机字符串,通过循环和字符池操作实现。重点讲解了如何使用`chars`变量、`RANDOM`和字符串切片来生成随机字符。
860

被折叠的 条评论
为什么被折叠?



