-
Notifications
You must be signed in to change notification settings - Fork 0
/
iqr.sh
155 lines (131 loc) · 4.03 KB
/
iqr.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
#!/usr/bin/env bash
# filename: iqr.sh
# author: [email protected]
export version=0.0.1
export verbose=0
logo() {
cat <<'EOF'
_____
\_ \_ __ ___ __ _ __ _ ___
/ /\/ '_ ` _ \ / _` |/ _` |/ _ \
/\/ /_ | | | | | | (_| | (_| | __/
\____/ |_| |_| |_|\__,_|\__, |\___|
|___/ Query Retrieval
EOF
}
help() {
logo
cat <<'EOF'
Usage:
iqr.sh [options] -e expression -l directory -o output.csv
-h, -help, --help Display help
-v, -version, --version Display version
-l, -location, --location Specify the root directory to start traversing the inode tree.
-e, -expression, --expression Specify Attribute Percent Escapes (https://imagemagick.org/script/escape.php)
to construct the expression for the output for each line; for example,
'%[basename]:%[colorspace]\n' will log a line something like 'tjn.jpg:sRGB'
-o, -output, --output Specify the output file; for example, output.csv.
-V, -verbose, --verbose Run script in verbose mode. Will print out each step of execution.
EOF
}
version() {
echo $version
}
promote() {
cat <<'EOF'
If you like it, please * this project on GitHub to make it known:
https://github.com/amughrabi/iqr
EOF
}
# `getopt` brief decryption command:
# $@ is all command line parameters passed to the script.
# -o is for short options like -v
# -l is for long options with double dash like --version
# the comma separates different long options
# -a is for long options with single dash like -version
# : is for mark the arg need a value; for example, if we need to make the venison accept value, we can change the -o
# value to hv:lVeo
options=$(getopt -l "help,version,location,verbose,expression,output" -o "hvl:Ve:o:" -a -- "$@")
# set --:
# If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters
# are set to the arguments, even if some of them begin with a ‘-’.
eval set -- "$options"
while true; do
case $1 in
-l|--location)
loc="$2"
shift
;;
-e|--expression)
expression="$2"
shift
;;
-o|--output)
output="$2"
;;
-h|--help)
help
exit 0
;;
-v|--version)
shift
version
exit 0
;;
-V|--verbose)
export verbose=1
set -xv # Set xtrace and verbose mode.
;;
*)
shift
break
;;
esac
shift
done
# Validation, empty.
if [[ ! -f "$loc" ]] && [[ ! -d "$loc" ]]; then
echo "ERROR: $loc DOES NOT exists."
exit 1
fi
if [[ -n "$output" ]] && [[ -d "$output" ]]; then
echo "WARN $output DOES exist."
exit 1
fi
if [[ -z $expression ]]; then
echo "$expression"
echo "ERROR: Missing expression value"
exit 1
fi
# Ok! All parameters are exist.
logo
echo "Scan : $loc"
if [[ -f $output ]]; then
echo "Output : $output"
echo "To See the results; Use : tail -f $output"
fi
start=`date +%s`
echo "Started :" $(date +'%d-%b-%Y-%I:%M:%S-%z')
if [[ -f "$loc" ]]; then
# if it is a file?
info=$(identify -format $expression $loc)
if [[ -f "$output" ]]; then
echo ${info} >> "$output"
else
echo 'Output :'
echo ${info}
fi
else
#
# Navigate the root directory
# Core code
find $loc -name '*' -exec file {} \; | grep -o -P '^.+: .* image' | cut -d ':' -f 1 | while read -r file; do info=$(identify -format $expression $file[0] | head -1); if [[ -n "$info" ]]; then echo ${info} >> $output; fi; done
fi
end=`date +%s`
if [[ -f "$output" ]]; then
echo "Done : $output"
fi
echo "Ended :" $(date +'%d-%b-%Y-%I:%M:%S-%z')
echo "Took :" `expr $end - $start` seconds.
promote
echo "Bye!!"