#!/bin/sh # $Id: drgenpass 44 2011-09-15 23:17:50Z jtk $ # http://dragonresearchgroup.org usage() { echo $0 [password_length] [number_of_passwords] ['character_set'] echo echo 'password_length positive integer, default=16, optional' echo 'number_of_passwords positive integer, default=1, optional' echo 'character_set string, default=[:graph:], optional' exit 1 } # is_positive_int() derived from Portable Shell Scripting, Blinn, p. 134 is_positive_int() { if [ $# -ne 1 ] then return 1 fi expr "$1" + 1 > /dev/null 2>&1 if [ $? -ge 2 ] then return 1 fi if [ $1 -lt 1 ] then return 1 fi return 0 } LEN=${1:-16} # arg1, length of password, optional NUM=${2:-1} # arg2, number of passwords to generate, optional SET=${3:-[:graph:]} # arg3, character class or set, optional is_positive_int $LEN || usage is_positive_int $NUM || usage # tr and fold technique derived from: # # NOTE: LANG=C is a hack for when tr is locale aware cat /dev/urandom | LANG=C tr -cd $SET | fold -w $LEN | head -n $NUM