rand (2821B)
1 #!/usr/bin/env bash 2 3 generate_string_azAZ09Symb() { 4 local length=$1 5 < /dev/urandom tr -dc 'A-Za-z0-9`~!@# $%^&*()-_=+[]{}|;:,.<>?/ ' | head -c "$length" 6 } 7 8 generate_string_azAZ09() { 9 local length=$1 10 < /dev/urandom tr -dc 'A-Za-z0-9' | head -c "$length" 11 } 12 13 print_help() { 14 echo "Usage: rand [OPTION]... [LENGTH]... [FILE]..." 15 echo "-n, --name [INT LENGTH] [FILE] renames a file with a random name" 16 echo "-c, --clip [INT LENGTH] copies a random string of specified length to clipboard" 17 echo "-h, --help display this help message" 18 } 19 20 clip() { 21 echo "$(generate_string_azAZ09Symb)" 22 } 23 24 name() { 25 local length=$1 26 local file=$2 27 28 if [[ -z "$file" || ! -f "$file" ]]; then 29 echo "ERROR file not found" 30 exit 1 31 fi 32 33 random_name=$(generate_string_azAZ09 "$length") 34 mv "$file" "$random_name" 35 echo "renamed '$file' to '$random_name'" 36 } 37 38 naming_option=0 39 clip_option=0 40 num_chars=0 41 words_option=0 42 target_file="" 43 44 while [[ "$1" =~ ^- ]]; do 45 case "$1" in 46 -n|--name) 47 naming_option=1 48 shift 49 if [[ -n "$1" && "$1" =~ ^[0-9]+$ ]]; then 50 num_chars=$1 51 shift 52 if [[ -n "$1" ]]; then 53 target_file=$1 54 shift 55 else 56 echo "no file provided" 57 exit 1 58 fi 59 else 60 echo "invalid or missing length for -n option" 61 echo "select -h option for help" 62 print_help 63 exit 1 64 fi 65 ;; 66 -c|--clip) 67 clip_option=1 68 shift 69 if [[ -n "$1" && "$1" =~ ^[0-9]+$ ]]; then 70 num_chars=$1 71 shift 72 else 73 echo "invalid or missing argument for -c option" 74 echo "select -h option for help" 75 print_help 76 exit 1 77 fi 78 ;; 79 -w|--words) 80 words_option=1 81 shift 82 if [[ -n "$1" && "$1" =~ ^[0-9]+$ ]]; then 83 num_words=$1 84 shift 85 else 86 echo "invalid or missing argument for -w option" 87 echo "select -h option for help" 88 print_help 89 exit 1 90 fi 91 ;; 92 -h|--help) 93 print_help 94 exit 0 95 ;; 96 *) 97 echo "ERROR invalid option: $1" 98 exit 1 99 ;; 100 esac 101 done 102 103 if [[ $naming_option -eq 1 ]]; then 104 if [[ -n "$num_chars" && -n "$target_file" ]]; then 105 name "$num_chars" "$target_file" 106 exit 0 107 else 108 echo "invalid or missing length for renaming" 109 print_help 110 exit 1 111 fi 112 elif [[ $clip_option -eq 1 ]]; then 113 if [[ -n "$num_chars" ]]; then 114 random_string=$(generate_string_azAZ09Symb "$num_chars") 115 echo "$random_string" | xclip -selection clipboard 116 echo "copied" 117 exit 0 118 else 119 print_help 120 exit 1 121 fi 122 elif [[ $words_option -eq 1 ]]; then 123 if [[ -n "$num_words" ]]; then 124 random_string=$(shuf -n $num_words /etc/dictionaries-common/words | tr '\n' ' ') 125 echo "$random_string" | xclip -selection clipboard 126 echo "copied" 127 exit 0 128 else 129 print_help 130 exit 1 131 fi 132 fi 133 134 if [[ $# -gt 0 && "$1" =~ ^[0-9]+$ ]]; then 135 num_chars=$1 136 random_string=$(generate_string_azAZ09Symb "$num_chars") 137 echo "$random_string" 138 exit 0 139 fi 140 141 print_help 142 exit 1