Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # gen v1.5.4 - Generates random patterns of characters with the option to exclude certain regions of the ASCII table.
- NOGO="0-32,34,39"
- SKIP_LIST=
- KEEP_LIST=
- RC=0
- CB=
- M=0
- typeset -i RND=33
- typeset -i CHK
- typeset -i START_N
- typeset -i END_N
- function ERROR() # display error message, infotext and exit with return code 1
- {
- printf "\n\033[0;31mError - $1\033[0;32m\n\n"
- cat <<EOF
- Usage: gen.bash -c [number of characters] -luns -e [start-end] -i [character(s)] -m
- -c [n] .............. number of random characters to be generated
- -l ................................... exclude lower case letters
- -u ................................... exclude upper case letters
- -n .............................................. exclude numbers
- -s ........................................ exclude special signs
- -e [start-end] .............. exclude a custom ASCII table region
- -i [character(s)] .................. include certain character(s)
- -m .......................... memorize (copy string to clipboard)
- If using the -e option, you may enter a single ASCII code or a whole range from start to end code with a minus "-"
- in between. You can also enter several codes and ranges, all separated by comma (i.e. -e 33,58-64,... and so on).
- The -i option will add characters on top of of the excluded ones meaning that they will be added even if they belong
- to an excluded group. Using this option you may enter a single character or a whole group in a single row.
- If you try to include special signs you may put them in quotes (single ' or double " while single quotes are more secure)
- to prevent the shell from misinterpreting them as function (i.e. -i '&/\' ). Thus single and double quotes can't be
- used as part of the random pattern at all. Remember that xclip must be installed in order to use the -m option.
- EOF
- printf "\033[0m\n"
- exit 1
- }
- function STRIP() # strip random number down to a printable ASCII code & check if it's supposed to be kept
- {
- while [ $RND -gt 126 ]; do RND=$RND/10; done
- IFS=","
- for RANGE in $NOGO; do
- START_N=${RANGE%-*}
- END_N=${RANGE#*-}
- if [ $RND -ge $START_N ] && [ $RND -le $END_N ]; then return 1; fi
- done
- for RANGE in $SKIP_LIST; do
- START_N=${RANGE%-*}
- END_N=${RANGE#*-}
- if [ $RND -ge $START_N ] && [ $RND -le $END_N ]; then
- RC=1
- if [ -n "$KEEP_LIST" ]; then
- for KEEP_NR in $KEEP_LIST; do
- if [ $RND -eq $KEEP_NR ]; then RC=0; fi
- done
- fi
- return $RC
- fi
- done
- }
- while getopts :c:C:e:E:i:I:lLuUnNsSmM OPT; do
- case $OPT in
- c|C)
- if [[ ! $OPTARG =~ ^[0-9]+$ ]]; then ERROR "C needs a number, nothing else!"; fi
- typeset -i LOOP=$OPTARG
- ;;
- e|E)
- if [[ ! $OPTARG =~ ^[0-9]+-?[0-9]+$ ]]; then ERROR "No valid ASCII code region."; fi
- SKIP_LIST=$SKIP_LIST","$OPTARG
- ;;
- i|I)
- KEEP_LIST=$(for CHR in $(echo $OPTARG | sed -e 's/\(.\)/\1\n/g'); do printf '%d' "'$CHR"; printf ","; done)
- ;;
- l|L)
- if [ -z $L ]; then
- SKIP_LIST=$SKIP_LIST"97-122,"
- L=1
- fi
- ;;
- u|U)
- if [ -z $U ]; then
- SKIP_LIST=$SKIP_LIST"65-90,"
- U=1
- fi
- ;;
- n|N)
- if [ -z $N ]; then
- SKIP_LIST=$SKIP_LIST"48-57,"
- N=1
- fi
- ;;
- s|S)
- if [ -z $S ]; then
- SKIP_LIST=$SKIP_LIST"33-47,58-64,91-96,123-126,"
- S=1
- fi
- ;;
- m|M)
- M=1
- ;;
- :)
- ERROR "$OPTARG requires an argument"
- ;;
- \?)
- ERROR "Unknown option: $OPTARG"
- ;;
- esac
- done
- if [ -z $LOOP ]; then ERROR "Number of characters MUST be given!"; fi
- while [ $RND -le 126 ]; do
- if STRIP; then CHK=$CHK+1; fi
- RND=$RND+1
- done
- if [ -z $CHK ]; then ERROR "Good job. You've just excluded the whole ASCII table. Try again!"; fi
- while [ $LOOP -gt 0 ]; do
- RND=$RANDOM
- if STRIP; then
- CHAR=$(printf "\\$(printf '%03o' "$RND")")
- echo -n "$CHAR"
- CB=$CB$CHAR
- LOOP=$LOOP-1
- fi
- done
- if [ $M -eq 1 ]; then
- if command -v xclip 1>/dev/null; then
- echo -n "$CB" | xclip -i -selection clipboard
- else
- printf "\n\033[0;31mUnable to copy string to clipboard - xclip not found.\033[0m"
- fi
- fi
- echo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement