Advertisement
pintcat

gen 1.5.4 (fixed bug in exclude option; more clean up)

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