Advertisement
pintcat

gen v1.6.5 - now fully POSIX compliant

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