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.