-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec6vid3Getopts
executable file
·52 lines (44 loc) · 1.19 KB
/
lec6vid3Getopts
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
#!/bin/bash
# this script generates a random password
# this user can set the password length with -l and 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"
echo " -l length specify the password length"
echo " -s append a special character to the password"
echo " -v increase verbosity"
exit 1
}
# set default password length
LENGTH=48
while getopts vl:s OPTION # vls because we are providing vls option , l: means l must have a argument on its own
do
case ${OPTION} in
v)
VERBOSE='true'
echo 'verbose mode on'
;;
l)
LENGTH="${OPTARG}"
;;
s)
USE_SPECIAL_CHARACTER=true
;;
?)
usage
;;
esac
done
# generaing password
echo "Generating password"
PASSWORD=$(date +%s%N$RANDOM | sha256sum | head -c${LENGTH})
# append a special character if requested to do so
if [ "${USE_SPECIAL_CHARACTER}" = 'true' ]
then
echo 'selecting a radom special charater'
SPECIAL_CHARACTER=$( echo '!@$%&()' | fold -w1 | shuf | head -c1 )
PASSWORD=$PASSWORD$SPECIAL_CHARACTER
fi
echo "DONE PASSWORD: ${PASSWORD}"
exit 0