#!/bin/bash
#
-# Copyright (C) 2008-2010, William Trevor King <wking@drexel.edu>
+# Copyright (C) 2008-2011, William Trevor King <wking@drexel.edu>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# to have lower NC (wastes less usable characters). For faster
# DEVICEs, it's better to have a higher NC (fewer slow shell loops).
-N=8 # password length
-NC=4 # number of characters in a "chunk"
+N="${1:-8}" # password length
+NC="${NC:-4}" # number of characters in a "chunk"
#SET="[:alnum:][:punct:]" # set of allowed characters (see tr(1))
-SET="[:alnum:]"
-DEVICE=/dev/random # use /dev/urandom for faster, weaker passwords
-
-if [ -n "$1" ]
-then
- N="$1"
-fi
+SET="${SET:-[:alnum:]}"
+DEVICE="${DEVICE:-/dev/random}" # use /dev/urandom for faster, weaker passwords
NA=0
PWD=""
-while [ "$N" -gt 0 ]; do
- ALL=$(head -c "$NC" "$DEVICE")
+while [ "${N}" -gt 0 ]; do
+ ALL=$(head -c "${NC}" "${DEVICE}")
let "NA = NA + ${#ALL}"
- NEW=$(echo "$ALL" | tr -d -c $SET | head -c "$N")
- PWD="$PWD$NEW"
+ NEW=$(echo -E "${ALL}" | tr -d -c "${SET}" | head -c "${N}")
+ PWD="${PWD}${NEW}"
let "N = N - ${#NEW}"
done
-echo "$PWD"
+echo "${PWD}"
echo "Length of password: ${#PWD}"
-echo "Total bytes read: $NA"
+echo "Total bytes read: ${NA}"
+echo "Device: ${DEVICE}"
+echo "Set: ${SET}"
exit 0