-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassgen.sh
71 lines (71 loc) · 1.87 KB
/
passgen.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
#!/bin/bash
#http://tldp.org/LDP/abs/html/contributed-scripts.html#PRIMES
#
#set -x
function usage() {
echo "Usage: $0 <length of password> <char> "
echo "second parameter is:"
echo " l - low case char"
echo " u - upper case chars"
echo " n - numbers"
echo " s- special chars"
echo "either altogether or in any combination"
echo " "
exit 0
}
Upperchars="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Lowchars="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
specialchars="!@#$%^&*()-_=+{}[]|~?><,~"
if [ $# -eq 2 ]; then
# $@ = stores all the arguments in a list of string
# $* = stores all the arguments as a single string
# $# = stores the number of arguments
secarg=$2
for (( i=0; i<${#secarg}; i++ ))
do
case "${secarg:$i:1}" in
"u")
MATRIX="$MATRIX $Upperchars"
;;
"l")
MATRIX="$MATRIX $Lowchars"
;;
"s")
MATRIX="$MATRIX $specialchars"
;;
"n")
MATRIX="$MATRIX $numbers"
;;
*)
usage
exit 1 #to fix problem with - line 61: 32021%0: division by 0 (error token is "0")
;;
esac
done
else
usage
fi
MATRIX=`echo -n $MATRIX | sed -e "s/ //g"`
LENGTH=$1
while [ "${n:=1}" -le "$LENGTH" ]
do
if [ "$n" -eq 1 ]
then
letter="${MATRIX:$(($RANDOM%${#MATRIX})):1}"
else
newletter="${MATRIX:$(($RANDOM%${#MATRIX})):1}"
while [ "$letter" == "$newletter" ]
do
newletter="${MATRIX:$(($RANDOM%${#MATRIX})):1}"
letter=$newletter
break
done
letter="$newletter"
fi
let n+=1
PASS="$PASS $letter"
PASS=`echo -n $PASS | sed -e "s/ //g"`
done
echo "$PASS"
exit 0