Solution is based on LINK. Stuff in use: cat, tr, fold, head, /dev/urandom, echo, printf, for each and parameters testing
#!/bin/bash
# settings the defaults
type=[:graph:]
length=32
qty=10
# checking and reassigning input wherever applicable
echo "Quantity of parameters passed: " $#
if [ -z $1 ] && [ -z $2 ] && [ -z $3 ]; then
printf "Using default values type: ${type}, length: ${length}, qty: ${qty} \n"
else
if [ "$#" -eq 3 ]; then
if [ "${1}" != "a" ] && [ "${2}" != "s" ] && [ "${3}" != "h" ]; then
printf "Using default values type: ${type}, length: ${length}, qty: ${qty} \n"
fi
if [ "${1}" == "a" ]; then
type=[:digit:][:alnum:]
echo "Selecting charset alphanumeric aka ${type}"
fi
if [ -n $1 ] && [ "${1}" == "s" ]; then
type=[:punct:][:digit:][:alnum:]
echo "Selecting charset alphanumeric + special chars aka ${type}"
fi
if [ -n $1 ] && [ "${1}" == "h" ]; then
type=[:xdigit:]
echo "Selecting charset hexadecimals aka ${type}"
fi
if [ -n $2 ] && [ "${2}" -gt 7 ] && [ "${2}" -lt 51 ]; then
length=$2
echo "Actual length is ${length}"
else echo "Value is not between 8 and 50, using default value for length: ${length}"
fi
if [ -n $3 ] && [ "${2}" -gt 0 ] && [ "${2}" -lt 51 ]; then
qty=$3
echo "Actual qty is ${qty}"
else echo "Value is not between 1 and 50, using default value for qty: ${qty}"
fi
else
printf "Using default values type: ${type}, length: ${length}, qty: ${qty} \n"
fi
fi
# generating strings
result=$(cat /dev/urandom | tr -dc ${type} | fold -w ${length} | head -n ${qty})
printf "\n== START ==\n"
for i in ${result}; do echo ${i}; done
printf "== END ==\n\n"
No comments:
Post a Comment