-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateRandomAlphaNumericString.sh
More file actions
executable file
·36 lines (24 loc) · 1.03 KB
/
Copy pathGenerateRandomAlphaNumericString.sh
File metadata and controls
executable file
·36 lines (24 loc) · 1.03 KB
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
36
#!/usr/bin/sh
# Generate a pseudo-random alphanumeric string of specified length
# Accepts one parameter: the length of the string to generate (range 1-32767) (default: 10)
# shellcheck disable=SC2155
GenerateRandomAlphaNumericString() {
char_list="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
result_length=${1:-10}
result_string=""
# If the length requested is outside sane upper and lower bounds, reset it
[ "$result_length" -lt 1 ] && result_length=1
[ "$result_length" -gt 32767 ] && result_length=32767
# Loop result_length times
while [ "${#result_string}" -lt "$result_length" ]
do
# Randomly choose one offset of length one from char_list
character=$(printf '%s' "$char_list" | cut -b$(( ($(shuf -i 1-32767 -n1) % 62) + 1 )) )
# Concatenate character onto result_string
result_string=${result_string}${character}
done
# "Return" the resulting string
printf '%s' "$result_string"
return 0
}
GenerateRandomAlphaNumericString "${1:-50}"