This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTF.sh
executable file
·474 lines (278 loc) · 10.3 KB
/
CTF.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/env bash
# Functions
function showMenu(){
local _full_text=${*}
local _cut_text=${_full_text/:-:0*/}
local _show
clear
echo -e "\n ${_cut_text}\n"
for(( _show = 1; _show < ${_full_text: -2}; _show++ )); do
_full_text=${_full_text/*:-:$(( _show - 1 ))/}
_cut_text=${_full_text/:-:${_show}*/}
echo -e "\t${txt_green}${_show})${txt_none} ${_cut_text}"
done
_full_text=${_full_text/*:-:$(( _show - 1 ))/}
echo -en "\t${txt_red}0)${txt_none} ${_full_text/${_full_text: -2}/}\n\n"
read -n1 -p " : " option
echo -e "\n"
return 0
}
function pause(){
local _in
while [[ -n ${1} ]]; do
_in=${1}
if [[ ${1,,} == -*beg* ]]; then
shift
local _beg_txt=${1}
shift
fi
if [[ ${1,,} == -*end* ]]; then
shift
local _end_txt=${1}
shift
fi
if [[ ${1,,} == -*t+([0-9]) ]]; then
local _time=${1,,//-*t/-t}
shift
fi
[[ ${_in} == ${1} ]] && \
shift
done
if [[ -n ${_beg_txt} && -n ${_end_txt} ]]; then
if [[ ${_beg_txt} = 0 ]]; then
clear
_beg_txt="Empty folder"
elif [[ ${_beg_txt} = -1 ]]; then
_beg_txt="Invalid value"
fi
if [[ ${_end_txt} = 0 ]]; then
_end_txt="exit"
elif [[ ${_end_txt} = 1 ]]; then
_end_txt="continue"
elif [[ ${_end_txt} = -1 ]]; then
clear
_end_txt="try again"
fi
read -sn1 ${_time} -p " ${_beg_txt}, type something to ${_end_txt}. . . " enterKey
else
read -sn1 ${_time} -p " Type something to continue. . . " enterKey
fi
echo ''
return 0
}
# Variables
# - - Text Style
txt_none="\033[m" # Normal Text
txt_red="\033[1;31m" # Cancel/Back
txt_green="\033[1;32m" # Normal Options
txt_yellow="\033[1;33m" # Lists
txt_blue="\033[1;36m" # Keys
txt_bold="\033[1;37m" # Notes to user
help_text="\n\t CTF.sh [-d DIR] [-ft PATTERN] [-fn FILE_NAME]\n\n------------------------------------------------------------------\n\n Description:\n\n\tBasic Command for creating a pdf file using text files.\n\n------------------------------------------------------------------\n\n Options:\n\n\b\t-d DIRECTORY\tDefines the folders where the files are.\n\n\t-ft FILTER\tCreate filter for files that will be used\n\t\t\tby the program.\n\n\t-fn NAME_FILE\tDefines the name of the file.\n\n\t-silent\t\tDon't show the options for editing the\n\t\t\tlist.\n\n\t-enumerate\tShow enumerates lines of files.\n\n------------------------------------------------------------------\n\n NOTE:\n\n\tOptions that depend on the passage of a text to the\n\tside, as is the case with -d, -fn, and among many\n\tothers must always be alone or at the end of a set\n\tof arguments, having at most one argument of this\n\ttype at a time.\n\n------------------------------------------------------------------\n\n\t\t\t /-----------------\ \n\t\t\t<| Type Q to quit. |>\n\t\t\t \-----------------/ \n"
# Set Main Variables
# help
[[ `echo ${*} | grep -ci 'help'` == 1 ]] && \
echo -e "${help_text}" | less && \
exit 0
# enumerate
[[ `echo ${*} | grep -ci 'enumerate'` == 1 ]] && \
enumerate=-n
# silent
[[ `echo ${*} | grep -ci 'silent'` == 1 ]] && \
silent=1
while [[ -n $1 ]]; do
_in=${1}
# output dir
if [[ ${1,,} == -*od* ]]; then
if [[ -z ${output_folder} ]]; then
shift
output_folder="${1}"
shift
else
echo -e "\n You cannot use more than one parent folder.\n"
exit 0
fi
# input dir
elif [[ ${1,,} == -*d* ]]; then
if [[ -z ${input_folder} ]]; then
shift
input_folder="${1}"
shift
else
echo -e "\n You cannot use more than one parent folder.\n"
exit 0
fi
fi
# filter
if [[ ${1,,} == -*ft* ]]; then
if [[ -z ${filter} ]]; then
shift
filter="${1}"
shift
else
echo -e "\n You cannot use more than one filter.\n"
exit 0
fi
fi
# file_name
if [[ ${1,,} == -*fn* ]]; then
if [[ -z ${file_name} ]]; then
shift
file_name="${1}"
shift
else
echo -e "\n You cannot use more than one file name.\n"
exit 0
fi
fi
[[ ${_in} == ${1} ]] && \
shift
done
unset _in
# Script
clear
# Empty Folder
[[ -z ${input_folder} ]] && \
read -p " In which folder do you want to run the program? " input_folder
# Invalid Folder
while [[ ! -d ${input_folder} ]]; do
clear
echo -en " Invalid folder (${txt_yellow}${input_folder}${txt_none}), in which folder do you want to run the program? "
read input_folder
done;
clear
cd ${input_folder}
# Choose whether or not to filter files
while [[ -z ${filter} && -z ${silent} ]]; do
clear
read -n1 -p ' Do you want to filter the files to be concatenated?(Y/N) ' option
case "${option,,}" in
y|1)
clear
read -p " Type the pattern at the beginning of the file or enter? " beg
clear
read -p " Type the pattern at the end of the file or enter? " end
filter=${beg}*${end}
;;
n|0) break ;;
*) pause -beg -1 -end -1 ;;
esac
done
unset option
clear
files=`ls ${filter}`
# Create List of Not Empty Text Files
for f in ${files}; do
[[ -s ${f} && `file -bi ${f//' '/'?'} | grep -c text` -eq 1 ]] && \
text_files[${#text_files[@]}]=${f}
done
# edit list (add, remove, continue, exit)
while [[ ${option} -ne 3 && -z ${silent} ]]; do
showMenu "You will create a pdf with the files:\n\n\t${txt_yellow}${text_files[@]}${txt_none}\n\n What do you want to do?:-:0Add a new element to the list.:-:1Removes an element from the list.:-:2Continue.:-:3Exit.04"
case "${option}" in
# Exit
0) exit 0 ;;
# Add
1)
clear
read -p " Enter the name of the new element: " files
clear
[[ -s ${files} && `file -bi ${files//' '/'?'} | grep -c text` -eq 1 ]] && \
text_files[${#text_files[@]}]=${files} || \
pause -beg "This item is not a text file or is empty" -end 1
;;
# Remove
2)
clear
if [[ -z ${text_files[*]} ]]; then
pause -beg "You can't remove files of the list because the text files list is empty" -end 1 -t10
else
until
echo -e "\n The text files list is:\n"
cont=0
for a in "${text_files[@]}"; do
let cont++
echo -e "\t${txt_green}${cont})${txt_none} ${a}"
done
unset cont
echo ''
read -p " Enter the number of the file to be removed from the list: " position
[[ ${position} -le 0 || ${position} -gt ${#text_files[*]} ]] && \
pause -beg -1 -end -1 && \
clear
[[ ${position} -ge 1 && ${position} -le ${#text_files[*]} ]]
do true ; done
unset text_files[$(( ${position} - 1 ))]
read -ra text_files -d '' <<< `echo "${text_files[@]}"`
fi
;;
# Continue
3)
clear
[[ -z ${text_files[*]} ]] && \
pause -beg "You can't continue because there are no itens in the list" -end -1 -t10 && \
option=5
;;
# Default
*) pause -beg -1 -end -1 ;;
esac
done
unset option
# Choose whether or not to enumerate the lines in the file
while [[ -z ${enumerate} && -z ${silent} ]]; do
clear
read -n1 -p ' Do you want to display the lines of the numbered files?(Y/N) ' option
case "${option,,}" in
y|1) enumerate=-n ;;
n|0) break ;;
*) pause -beg -1 -end -1 ;;
esac
done
unset option
clear
# Choose the File Name
while [[ -z ${file_name} ]]; do
read -p " What is the file name? " file_name
if [[ -z ${file_name} ]]; then
pause -beg -1 -end -1
clear
fi
done
# Define output folder
while [[ -z ${output_folder} && -z ${silent} ]]; do
clear
read -n1 -p ' Do you want to define where the pdf file should be created?(Y/N) ' option
case "${option,,}" in
y|1)
clear
# Empty Folder
[[ -z ${output_folder} ]] && \
read -p " In which folder do you want to create the pdf file? " output_folder
# Invalid Folder
while [[ ! -d ${output_folder} ]]; do
clear
echo -en " Invalid folder (${txt_yellow}${output_folder}${txt_none}), in which folder do you want to create the pdf file? "
read output_folder
done;
;;
n|0) break ;;
*) pause -beg -1 -end -1 ;;
esac
done
# Create and edit text file
touch ${file_name}.txt
i=1
for f in ${text_files[@]}; do
echo -e "ARQUIVO ${i}: ${f}\n" >> ${file_name}.txt 2> /dev/null
cat ${enumerate} ${f} >> ${file_name}.txt 2> /dev/null
echo -e "\n\n\n" >> ${file_name}.txt 2> /dev/null
let i++
done
unset i
# txt2pdf
libreoffice --convert-to pdf ${file_name}.txt &> /dev/null
# Remove Temp File
rm ${file_name}.txt
[[ -n ${output_folder} ]] && \
mv ${file_name}.pdf ${output_folder// /?} 2> /dev/null
exit 0