shellでランダム文字列を作成する方法
1.shellで20桁のランダム文字列を作成する
#!/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`
2.shellでランダムもじれるを作成
#!/bin/bash
MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+="
LENGTH="9″
while [ “${n:=1}" -le “$LENGTH" ]
do
PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
let n+=1
done
echo “$PASS"
exit 0