-
Notifications
You must be signed in to change notification settings - Fork 0
/
x_on_files.sh
executable file
·60 lines (42 loc) · 1.17 KB
/
x_on_files.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
#!/bin/bash
function execute_on_files_recursive() {
directory="${1}"
for file_entry in "${directory}"/*
do
if [ -d "${file_entry}" ]
then
execute_on_files_recursive "${file_entry}"
elif [ -f "${file_entry}" ] && [[ "${file_entry}" =~ \.?${suffix}$ ]]
then
if [ $args_count -eq 2 ]
then
${program} "${file_entry}"
else
${program} "${file_entry}" "${further_program_args}"
fi
fi
done
}
function safety_break() {
local x=$(shuf -i 1-9 -n 1)
read -r -p "Enter '${x}' to confirm.. " INPUT
if [ "${INPUT}" != "${x}" ] # user entered nothing or something else than x
then
echo "Execution stopped."
exit 0
fi
}
############ START:
program="${1}"
suffix="${2}"
further_program_args="${3}"
args_count="${#}"
if [[ ${#} -lt 2 ]] || [[ ${#} -gt 3 ]]
then
echo "Usage: x_on_files.sh program suffix [further program arguments]"
exit 1
fi
echo "You are going to execute the following on all *${suffix} files that can be found in the current directory and its subdirectories recursively:"
echo "${program} [*${suffix} file] ${further_program_args}"
safety_break
execute_on_files_recursive "."