-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase-statements.sh
More file actions
94 lines (71 loc) · 1.32 KB
/
case-statements.sh
File metadata and controls
94 lines (71 loc) · 1.32 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# Demonstration of the case statement.
#case "${1}" in
# start)
# echo 'Starting'
# ;;
# stop)
# echo 'Stopping'
# ;;
# status|state)
# echo 'Status: '
# ;;
# *)
# echo 'Supply a valid option.' >&2
# exit 1
# ;;
#esac
# Function demonstration
#log(){
# local MESSAGE="${@}"
# echo "${MESSAGE}"
#}
#log 'Hello!'
#log 'This is fun!'
# Random password generate
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
}
display(){
local MESSAGE="${@}"
if [[ "${VERBOSE}" = 'true' ]]
then
echo "${MESSAGE}"
fi
}
LENGTH=16
while getopts vl:s OPTION
do
case ${OPTION} in
v)
VERBOSE='true'
display 'Verbose mode on.'
;;
l)
LENGTH="${OPTARG}"
;;
s)
SPEICAL_CHAR='true'
;;
?)
usage
;;
esac
done
display 'Generating a password.'
PASSWORD=$(date +%s%N${RANDOM}${RANDOM} | sha256sum | head -c${LENGTH})
if [[ "${SPECIAL_CHAR}" = 'true' ]]
then
display 'Selecting a random special character.'
SPECIAL_CHARACTER=$(echo '!@#$%^&*()-+=' | fold -w1 | shuf | head -c1)
PASSWORD="${PASSWORD}${SPECIAL_CHARACTER}"
fi
display 'DONE'
display 'Here is the password - '
echo "${PASSWORD}"
exit 0