rand (2237B)
1 #!/bin/sh 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 "File not found or not provided." 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 target_file="" 42 43 while [[ "$1" =~ ^- ]]; do 44 case "$1" in 45 -n|--name) 46 naming_option=1 47 shift 48 if [[ -n "$1" && "$1" =~ ^[0-9]+$ ]]; then 49 num_chars=$1 50 shift 51 if [[ -n "$1" ]]; then 52 target_file=$1 53 shift 54 else 55 echo "No file specified for renaming." 56 exit 1 57 fi 58 else 59 echo "Invalid or missing length for -n option." 60 print_help 61 exit 1 62 fi 63 ;; 64 -c|--clip) 65 clip_option=1 66 shift 67 if [[ -n "$1" && "$1" =~ ^[0-9]+$ ]]; then 68 num_chars=$1 69 shift 70 else 71 echo "Invalid or missing argument for -c option." 72 print_help 73 exit 1 74 fi 75 ;; 76 -h|--help) 77 print_help 78 exit 0 79 ;; 80 *) 81 echo "Invalid option: $1" 82 exit 1 83 ;; 84 esac 85 done 86 87 if [ $naming_option -eq 1 ]; then 88 if [[ -n "$num_chars" && -n "$target_file" ]]; then 89 name "$num_chars" "$target_file" 90 exit 0 91 else 92 echo "Missing length or file for renaming." 93 print_help 94 exit 1 95 fi 96 elif [ $clip_option -eq 1 ]; then 97 if [[ -n "$num_chars" ]]; then 98 random_string=$(generate_string_azAZ09Symb "$num_chars") 99 echo "$random_string" | xclip -selection clipboard 100 echo "copied to clipboard" 101 exit 0 102 else 103 print_help 104 exit 1 105 fi 106 fi 107 108 if [[ $# -gt 0 && "$1" =~ ^[0-9]+$ ]]; then 109 num_chars=$1 110 random_string=$(generate_string_azAZ09Symb "$num_chars") 111 echo "$random_string" 112 exit 0 113 fi 114 115 print_help 116 exit 1