#!/bin/sh # # Copyright (c) 2007 mmw # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # # # doword # # Created by plumber on 11/30/07. # Copyright 2007 OpenSpecies. All rights reserved. # # MAX_LEN=64 MIN_LEN=2 function isint() { [ "${1}" -eq "${1}" ] > /dev/null 2>&1 return $? } function randword () { randword=`cat /dev/random | head -n 1 | tr -cd \"[:${1}:]\" | tail -c ${2}` } function randrealword () { r=$(wc -l < /usr/share/dict/words) randrealword=`sed $(perl -e "print int(rand($r))")"q;d" /usr/share/dict/words | tail -c ${1}` } function doword () { if test "$mode" = "word" ; then while [ 1 ]; do randrealword `expr ${2} + 1` if [ "${#randrealword}" -ge "$MIN_LEN" ]; then break fi done ret=$randrealword else while [ 1 ]; do randword "${1}" "${2}" if [ "${#randword}" -eq "${2}" ]; then break fi done if test "$mode" = "alnum" ; then ret=$(echo $randword | tr '\"' 1) elif test "$mode" = "alpha" ; then ret=$(echo $randword | tr '\"' a) else ret=$randword fi fi doword=$ret } function usage () { cat < word_upper (upper-case alphabetic characters) word_lower (lower-case alphabetic characters) alnum alnum_upper (upper-case alphabetic characters) alnum_lower (lower-case alphabetic characters) alpha alpha_upper (upper-case alphabetic characters) alpha_lower (lower-case alphabetic characters) print print_upper (upper-case alphabetic characters) print_lower (lower-case alphabetic characters) EOF exit 1 } while getopts ":m:l:h" opt do case $opt in m) case $OPTARG in word) mode="word" mode_set=yes ;; word_upper) mode="word" mode_set=yes upper_set=yes ;; word_lower) mode="word" mode_set=yes lower_set=yes ;; alnum) mode="alnum" mode_set=yes ;; alnum_upper) mode="alnum" mode_set=yes upper_set=yes ;; alnum_lower) mode="alnum" mode_set=yes lower_set=yes ;; alpha) mode="alpha" mode_set=yes ;; alpha_upper) mode="alpha" mode_set=yes upper_set=yes ;; alpha_lower) mode="alpha" mode_set=yes lower_set=yes ;; print) mode="print" mode_set=yes ;; print_upper) mode="print" mode_set=yes upper_set=yes ;; print_lower) mode="print" mode_set=yes lower_set=yes ;; *) mode="word" ;; esac ;; l) if isint $OPTARG; then if [ "$OPTARG" -gt "$MAX_LEN" ]; then len=$MAX_LEN len_set=yes elif [ "$OPTARG" -lt "$MIN_LEN" ]; then len=$MIN_LEN len_set=yes else len=$OPTARG len_set=yes fi else echo "$0: option -l requires an integer argument" exit 1 fi ;; *) usage ;; esac done if ! test "$mode_set" = "yes" ; then mode="word" fi if ! test "$len_set" = "yes" ; then len=$MAX_LEN fi doword $mode $len if test "$upper_set" = "yes" ; then ret=$(echo $doword | tr a-z A-Z) elif test "$lower_set" = "yes" ; then ret=$(echo $doword | tr A-Z a-z ) else ret=$doword fi echo $ret # EOF