Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # gen v1.6.6 - Generates random patterns of characters with the option to exclude certain regions of the ASCII table.
- 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.sh -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)
- -f ......................... no linefeed at the end of the string
- -d [string] .... put [string] between each character as delimiter
- -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
- }
- IPATH="/home/pintcat/skripte/"
- [ -x "$IPATH"whrandom.sh ] || ERROR "whrandom.sh is either missing or not executable."
- . "$IPATH"whrandom.sh
- IFS=$IFS","
- WH_LOOP=1000
- WH_ALGO=WH_OLD
- WH_MAX=126
- WH_DEL=" "
- NOGO="0-32,34,39"
- RC=0
- RND=33
- STRIP(){ # strip random number down to a printable ASCII code & check if it's supposed to be kept
- for RANGE in $NOGO; do
- START_N=${RANGE%-*}
- END_N=${RANGE#*-}
- [ $RND -ge $START_N -a $RND -le $END_N ] && return 1
- done
- for RANGE in $SKIP_LIST; do
- START_N=${RANGE%-*}
- END_N=${RANGE#*-}
- if [ $RND -ge $START_N -a $RND -le $END_N ]; then
- RC=1
- [ -n "$KEEP_LIST" ] && for KEEP_NR in $KEEP_LIST; do
- [ $RND -eq $KEEP_NR ] && RC=0
- done
- return $RC
- fi
- done
- }
- while getopts :c:C:d:D:e:E:i:I:lLuUnNsSfFmM OPT; do
- case $OPT in
- c|C)
- [ -z "${OPTARG##*[!0-9]*}" ] && ERROR "C needs a number, nothing else!"
- LOOP=$OPTARG
- ;;
- d|D)
- DEL="$OPTARG"
- ;;
- e|E)
- expr "$OPTARG" : '^[0-9]\{1,\}-[0-9]\{1,\}$' > /dev/null || ERROR "No valid ASCII code region."
- 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)
- [ $((L=$L+1)) -eq 1 ] && SKIP_LIST=$SKIP_LIST"97-122,"
- ;;
- u|U)
- [ $((U=$U+1)) -eq 1 ] && SKIP_LIST=$SKIP_LIST"65-90,"
- ;;
- n|N)
- [ $((N=$N+1)) -eq 1 ] && SKIP_LIST=$SKIP_LIST"48-57,"
- ;;
- s|S)
- [ $((S=$S+1)) -eq 1 ] && SKIP_LIST=$SKIP_LIST"33-47,58-64,91-96,123-126,"
- ;;
- f|F)
- LFEED=1
- ;;
- m|M)
- M=1
- ;;
- :)
- ERROR "$OPTARG requires an argument"
- ;;
- \?)
- ERROR "Unknown option: $OPTARG"
- ;;
- esac
- done
- [ -z $LOOP ] && ERROR "Number of characters MUST be given!"
- while [ $RND -le 126 ]; do
- STRIP && CHK=$(($CHK+1))
- RND=$(($RND+1))
- done
- [ -z $CHK ] && ERROR "Good job. You've just excluded the whole ASCII table. Try again!"
- while [ $LOOP -gt 0 ]; do
- ARRAY=$(WH_RANDOM)
- for RND in $ARRAY; do
- if STRIP; then
- [ $LOOP -eq 1 ] && DEL=
- CHAR=$(printf "\\$(printf '%03o' "$RND")")"$DEL"
- printf "%s" "$CHAR"
- CB=$CB$CHAR
- [ $((LOOP=$LOOP-1)) -eq 0 ] && break
- fi
- done
- done
- if [ -n "$M" ]; then
- if command -v xclip 1>/dev/null; then
- printf "$CB" | xclip -i -selection clipboard
- printf "\nString was copied to clipboard."
- else
- printf "\n\033[0;31mUnable to copy string to clipboard - xclip not found.\033[0m"
- fi
- fi
- [ -z $LFEED ] && echo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement