This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luser-demo11.sh
72 lines (59 loc) · 1.41 KB
/
luser-demo11.sh
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# This script generates a random password.
# The user can set the password length with -l and add a special character with -s.
# Verbose mode can be enabled with -v.
usage() {
echo "Usage: ${0} [-vs] [-l LENGTH]" >&2
echo 'Generate a random password.' >&2
echo ' -l LENGTH Specify the password length.' >&2
echo ' -s Append a special character to the password.' >&2
echo ' -v Increase verbosity.' >&2
exit 1
}
log() {
local MESSAGE="${@}"
if [[ "${VERBOSE}" = 'true' ]]
then
echo "${MESSAGE}"
fi
}
# Set a default password length.
LENGTH=48
while getopts vl:s OPTION
do
case ${OPTION} in
v)
VERBOSE='true'
log 'Verbose mode on.'
;;
l)
LENGTH="${OPTARG}"
;;
s)
USE_SPECIAL_CHARACTER='true'
;;
?)
usage
;;
esac
done
# Remove the options while leaving the remaining arguments.
shift "$(( OPTIND - 1 ))"
if [[ "${#}" -gt 0 ]]
then
usage
fi
log 'Generating a password.'
PASSWORD=$(date +%s%N${RANDOM}${RANDOM} | sha256sum | head -c${LENGTH})
# Append a special character if requested to do so.
if [[ "${USE_SPECIAL_CHARACTER}" = 'true' ]]
then
log 'Selecting a random special character.'
SPECIAL_CHARACTER=$(echo '!@#$%^&*()_-+=' | fold -w1 | shuf | head -c1)
PASSWORD="${PASSWORD}${SPECIAL_CHARACTER}"
fi
log 'Done.'
log 'Here is the password:'
# Display the password.
echo "${PASSWORD}"
exit 0