Advertisement
pintcat

gen v1.6.7 - now fully POSIX compliant

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