-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsConv
executable file
·150 lines (128 loc) · 3.21 KB
/
lsConv
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
#!/bin/bash
# lsConv - Files Lists Converter
#
# Author: Gil Stolar
# OUR ORIGINAL CODE THAT WAS WRITTEN BY THE SECOPX LTD TEAM IS RELEASED UNDER GPL3!
# Copyright (C) 2021 Secopx.com
VERSION="1.0"
TMPFILE="tmp_list1.tmp"
TMPFILE2="tmp_list2.tmp"
POS=()
while [[ $# -gt 0 ]]
do
opt="$1"
case $opt in
-re|--regex)
REGEX=1
shift
;;
-js|--json)
JSON=1
shift
;;
-f|--file)
INPUT_FILE="$2"
shift
shift
;;
-d|--directory)
DIRECTORY="$2"
shift
shift
;;
-r|--recursive)
RECURSE="1"
shift
;;
-o|--output-file)
OUTPUT_FILE="$2"
shift
shift
;;
-h|--help)
HELP=1
shift
shift
;;
-v|--version)
echo "$VERSION"
exit
shift
;;
*) # unknown option
POS+=("$1") # save it in an array for later
shift
;;
esac
done
set -- "${POS[@]}" # restore positional parameters
if [[ -n $1 ]]; then echo "ERROR: Invalid option:"; tail -1 "$1"; exit 0; fi
HELP_TXT="
usage: ./lsConv [-f | --file <path>] [-d | --directory <path>]
[-r | --recursive] [-re | --regex] [-js | --json]
[-o | --output <path>]
[-h | --help]
Converts a list of files from a directory or a text file to a chosen format
-d, --directory converts files from selected folder, -d <path>
-f, --file converts files listed in a file, -f <path>
(supports * wildcard)
-js, --json converts list to json format
-o, --output-file prints converted list to a file, -o <path>
-r, --recursive with -d, list all files in sub-folders
-re, --regex converts list to regex
-h, --help display this help and exit
-v, --version output version information and exit
"
function print_help(){
printf "\n${HELP_TXT}\n\n"
exit 0
}
#function check_cmd_params(){
# if [ ! -f $1 ]; then echo "ERROR: File not exists"; exit 0; fi
# if [ "$1" == "" ]; then echo "ERROR: Missing argument: path to file."; print_help; fi
#}
if [[ $HELP ]]; then print_help; fi
## Initialize
#check_cmd_params ${INPUT_FILE}
if [[ $DIRECTORY ]];then
if [ -f $TMPFILE2 ];then touch ${TMPFILE2}; else echo "">$TMPFILE2; fi
if [[ ! $RECURSE ]];then opts="-maxdepth 1"; fi
find ${DIRECTORY} ${opts} -type f >$TMPFILE2
INPUT_FILE=${TMPFILE2}
fi
if [ -f $TMPFILE ];then touch ${TMPFILE}; else echo "">$TMPFILE; fi
fnList=$(< ${INPUT_FILE})
echo "$fnList" > $TMPFILE
if [[ $REGEX ]]; then
sed -i -e 's/^/\|/' $TMPFILE
sed -i -e '1s/^.//' $TMPFILE
sed -i -e "s/[\!\@\#\$\%\^\&\(\)\-\/\|\.]/\\\&/g" $TMPFILE
sed -i -e "s/\*/.*/" $TMPFILE
output=$(cat $TMPFILE)
output="${output//$'\n'/}"
fi
if [[ $JSON ]]; then
if [[ $REGEX ]];then
printf "${output}" >$TMPFILE
output='{"files_list": ["'
sed -i -e 's/|/","/g' $TMPFILE
output+="$(< $TMPFILE)\"]}"
printf "${output}\n"
exit
else
output='{"files_list": ['
while read fn; do output+="\"${fn}\","; done <$TMPFILE
output=${output::-1}
output+=']}'
fi
fi
## Proccess output to a file or print to stdout
if [ $OUTPUT_FILE ]; then
if [ -f $OUTPUT_FILE ];then touch ${OUTPUT_FILE}; else echo "">$OUTPUT_FILE; fi
printf "${output}" > $OUTPUT_FILE
else
printf "${output}\n"
fi
rm $TMPFILE
if [ -f $TMPFILE2 ]; then rm $TMPFILE2; fi
#EOF