toyotacorolla

vroom
git clone https://git.davidvoz.net/toyotacorolla.git
index
logs
tree

rand
1#!/usr/bin/env bash
2
3print_help() {
4 echo "Usage: rand [OPTION]... [LENGTH]... [FILE]..."
5 echo "-n, --name [INT LENGTH] [FILE] renames a file with a random name"
6 echo "-c, --clip [INT LENGTH] copies a random string of specified length to clipboard"
7 echo "-h, --help display this help message"
8}
9
10generate_string_azAZ09Symb() {
11 local length=$1
12 < /dev/urandom tr -dc 'A-Za-z0-9`~!@# $%^&*()-_=+[]{}|;:,.<>?/ ' | head -c "$length"
13}
14
15generate_string_azAZ09() {
16 local length=$1
17 < /dev/urandom tr -dc 'A-Za-z0-9' | head -c "$length"
18}
19
20clip() {
21 echo "$(generate_string_azAZ09Symb)"
22}
23
24name() {
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
38naming_option=0
39clip_option=0
40num_chars=0
41words_option=0
42target_file=""
43
44while [[ "$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
101done
102
103if [[ $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
112elif [[ $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
122elif [[ $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
132fi
133
134if [[ $# -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
139fi
140
141print_help
142exit 1