forked from MKessar/GameShell_git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_string
More file actions
executable file
·35 lines (31 loc) · 941 Bytes
/
random_string
File metadata and controls
executable file
·35 lines (31 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/sh
# usage
# random_string [N [ALPHABET]]
#
GSH_ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
GSH_CONFIG=$GSH_ROOT/.config
n=$1
alpha=$(echo "${2:-"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}" | sed 's/./& /g')
awk -v n=${n:-32} -v alpha="$alpha" -v seed_file="$GSH_CONFIG/PRNG_seed" -v PID=$$ 'BEGIN {
getline seed < seed_file;
if (seed) srand(seed); else srand();
l = split(alpha, chars);
for(i=0;i<n;i++) {
printf("%c", chars[1+int(rand()*l)]);
}
printf("\n");
if (seed) printf("%d\n", int(2^30 * rand())+PID) > seed_file;
}'
# make sure the seed is not corrupted / empty
# (it could happen if the process is interrupted at the wrong time)
if [ -d "$GSH_CONFIG" ]
then
case "$(cat "$GSH_CONFIG/PRNG_seed" 2>/dev/null)" in
"" | *[!0-9]*)
awk -v seed_file="$GSH_CONFIG/PRNG_seed" 'BEGIN { srand(); printf("%s", int(2^32 * rand())) > seed_file; }'
;;
*)
:
;;
esac
fi