Advertisement
pintcat

gen v1.6.6 - now fully POSIX compliant

Jan 1st, 2024 (edited)
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.29 KB | Software | 0 0
  1. #!/bin/sh
  2.  
  3. # gen v1.6.6 - Generates random patterns of characters with the option to exclude certain regions of the ASCII table.
  4.  
  5. ERROR(){ # display error message, infotext and exit with return code 1
  6.     printf "\n\033[0;31mError - $1\033[0;32m\n\n"
  7.     cat <<EOF
  8.  Usage: gen.sh -c [number of characters] -luns -e [start-end] -i [character(s)] -m
  9.  
  10.     -c [n] .............. number of random characters to be generated
  11.     -l ................................... exclude lower case letters
  12.     -u ................................... exclude upper case letters
  13.     -n .............................................. exclude numbers
  14.     -s ........................................ exclude special signs
  15.     -e [start-end] .............. exclude a custom ASCII table region
  16.     -i [character(s)] .................. include certain character(s)
  17.     -f ......................... no linefeed at the end of the string
  18.     -d [string] .... put [string] between each character as delimiter
  19.     -m .......................... memorize (copy string to clipboard)
  20.  
  21.  If using the -e option, you may enter a single ASCII code or a whole range from start to end code with a minus "-"
  22.  in between. You can also enter several codes and ranges, all separated by comma (i.e. -e 33,58-64,... and so on).
  23.  The -i option will add characters on top of of the excluded ones meaning that they will be added even if they belong
  24.  to an excluded group. Using this option you may enter a single character or a whole group in a single row.
  25.  If you try to include special signs you may put them in quotes (single ' or double " while single quotes are more secure)
  26.  to prevent the shell from misinterpreting them as function (i.e. -i '&/\' ). Thus single and double quotes can't be
  27.  used as part of the random pattern at all. Remember that xclip must be installed in order to use the -m option.
  28. EOF
  29.     printf "\033[0m\n"
  30.     exit 1
  31. }
  32.  
  33. IPATH="/home/pintcat/skripte/"
  34. [ -x "$IPATH"whrandom.sh ] || ERROR "whrandom.sh is either missing or not executable."
  35. . "$IPATH"whrandom.sh
  36.  
  37. IFS=$IFS","
  38. WH_LOOP=1000
  39. WH_ALGO=WH_OLD
  40. WH_MAX=126
  41. WH_DEL=" "
  42. NOGO="0-32,34,39"
  43. RC=0
  44. RND=33
  45.  
  46. STRIP(){ # strip random number down to a printable ASCII code & check if it's supposed to be kept
  47.     for RANGE in $NOGO; do
  48.         START_N=${RANGE%-*}
  49.         END_N=${RANGE#*-}
  50.         [ $RND -ge $START_N -a $RND -le $END_N ] && return 1
  51.     done
  52.     for RANGE in $SKIP_LIST; do
  53.         START_N=${RANGE%-*}
  54.         END_N=${RANGE#*-}
  55.         if [ $RND -ge $START_N -a $RND -le $END_N ]; then
  56.             RC=1
  57.             [ -n "$KEEP_LIST" ] && for KEEP_NR in $KEEP_LIST; do
  58.                 [ $RND -eq $KEEP_NR ] && RC=0
  59.             done
  60.             return $RC
  61.         fi
  62.     done
  63. }
  64.  
  65. while getopts :c:C:d:D:e:E:i:I:lLuUnNsSfFmM OPT; do
  66.     case $OPT in
  67.     c|C)
  68.         [ -z "${OPTARG##*[!0-9]*}" ] && ERROR "C needs a number, nothing else!"
  69.         LOOP=$OPTARG
  70.         ;;
  71.     d|D)
  72.         DEL="$OPTARG"
  73.         ;;
  74.     e|E)
  75.         expr "$OPTARG" : '^[0-9]\{1,\}-[0-9]\{1,\}$' > /dev/null || ERROR "No valid ASCII code region."
  76.         SKIP_LIST=$SKIP_LIST$OPTARG","
  77.         ;;
  78.     i|I)
  79.         KEEP_LIST=$(for CHR in $(echo $OPTARG | sed -e 's/\(.\)/\1\n/g'); do printf '%d' "'$CHR"; printf ","; done)
  80.         ;;
  81.     l|L)
  82.         [ $((L=$L+1)) -eq 1 ] && SKIP_LIST=$SKIP_LIST"97-122,"
  83.         ;;
  84.     u|U)
  85.         [ $((U=$U+1)) -eq 1 ] && SKIP_LIST=$SKIP_LIST"65-90,"
  86.         ;;
  87.     n|N)
  88.         [ $((N=$N+1)) -eq 1 ] && SKIP_LIST=$SKIP_LIST"48-57,"
  89.         ;;
  90.     s|S)
  91.         [ $((S=$S+1)) -eq 1 ] && SKIP_LIST=$SKIP_LIST"33-47,58-64,91-96,123-126,"
  92.         ;;
  93.     f|F)
  94.         LFEED=1
  95.         ;;
  96.     m|M)
  97.         M=1
  98.         ;;
  99.     :)
  100.         ERROR "$OPTARG requires an argument"
  101.         ;;
  102.     \?)
  103.         ERROR "Unknown option: $OPTARG"
  104.         ;;
  105.     esac
  106. done
  107.  
  108. [ -z $LOOP ] && ERROR "Number of characters MUST be given!"
  109.  
  110. while [ $RND -le 126 ]; do
  111.     STRIP && CHK=$(($CHK+1))
  112.     RND=$(($RND+1))
  113. done
  114. [ -z $CHK ] && ERROR "Good job. You've just excluded the whole ASCII table. Try again!"
  115.  
  116. while [ $LOOP -gt 0 ]; do
  117.     ARRAY=$(WH_RANDOM)
  118.     for RND in $ARRAY; do
  119.         if STRIP; then
  120.             [ $LOOP -eq 1 ] && DEL=
  121.             CHAR=$(printf "\\$(printf '%03o' "$RND")")"$DEL"
  122.             printf "%s" "$CHAR"
  123.             CB=$CB$CHAR
  124.             [ $((LOOP=$LOOP-1)) -eq 0 ] && break
  125.         fi
  126.     done
  127. done
  128. if [ -n "$M" ]; then
  129.     if command -v xclip 1>/dev/null; then
  130.         printf "$CB" | xclip -i -selection clipboard
  131.         printf "\nString was copied to clipboard."
  132.     else
  133.         printf "\n\033[0;31mUnable to copy string to clipboard - xclip not found.\033[0m"
  134.     fi
  135. fi
  136. [ -z $LFEED ] && echo
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement