-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_test.sh
211 lines (179 loc) · 5.3 KB
/
bash_test.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
# !/bin/bash
#Konrad Grzyb
#227305 EITI
#version 01.06.2017
help="HELP:
Write a script modify with the following syntax:
modify [-r] [-l|-u] <dir/file names...>
modify [-r] <sed pattern> <dir/file names...>
modify [-h]
which will modify file names. The script is dedicated to lowerizing (-l)
file names, uppercasing (-u) file names or internally calling sed
command with the given sed pattern which will operate on file names.
Changes may be done either with recursion (-r) or without it. -r doesn't take arguments so it's a standalone option.
Write a second script, named modify_examples, which will lead the tester of the modify script through the typical, uncommon and even incorrect scenarios of usage of the modify script.
(-u) - uppercasing all file names and directories
(-l) - lowerizing all file names and directories
(-r) - recursively change file and directory names with given options -u or -l
(-h) - help
usage: modify [-r] [-l|-u] <dir/file names...> || modify [-r] <sed pattern> <dir/file names...> || modify [-h]";
#------------------------------------------------------funcitons declaration
function getArguments() { #need pass array
for (( i = 0; i < ${#all_args[*]}; i++ )); do
if [ ${all_args[i]} != '-r' ] && [ ${all_args[i]} != '-l' ] && [ ${all_args[i]} != '-u' ]; then
if [ -f ${all_args[i]} ] || [ -d ${all_args[i]} ]; then #if file or directory
args[0]=${all_args[i]}; #store path
elif [[ -n ${all_args[i]} ]]; then # proper sed?
args[1]=${all_args[i]}; #store sed pattern
fi
fi
done
}
function changeName() {
old_path=$(dirname "$1")/$(basename "$1");
if [[ ${param[1]} == u ]]; then
change_base=`echo $(basename "$1") |cut -c1 |tr '[:lower:]' '[:upper:]'``echo $(basename "$1") |cut -c2-`;
else
change_base=`echo $(basename "$1") |cut -c1 |tr '[:upper:]' '[:lower:]'``echo $(basename "$1") |cut -c2-`;
fi
new_path=$(dirname "$1")/$change_base;
#change name
mv -f "$old_path" "$new_path";
}
function changeNames() { #$1 -> path
echo "changing names for path: $1";
find $1 -maxdepth 1 -type f |while read i; do
changeName "$i";
done
}
function changeNamesWithRecursion() { #$1 -> path
echo "changing names with -r for path: $1";
find $1 -type f |while read i; do
changeName "$i";
done
}
function changeSedNames() { #$1 -> path, $2 -> pattern
echo "changing names with sed for path: $1";
find $1 -maxdepth 1 -type f |while read i; do
sed -s $2;
done
}
function changeSedNamesWithRecursion() { #$1 -> path, $2 -> pattern
echo "changing names with sed and -r for path: $1";
find $1 -type f |while read i; do
sed -s $2;
done
}
function performOption {
if [[ ${#args[*]} == 0 ]]; then
#option and no argumetns
changeNames $PWD;
else
if test "${args[0]+isset}" ; then
changeNames ${args[0]};
else
#there is an argument and not a path
echo "error - give path not sed";
exit;
fi
fi
}
function performOptionWithRecursion {
if [[ ${#args[*]} == 0 ]]; then
#option and no argumetns
changeNamesWithRecursion $PWD;
else
if test "${args[0]+isset}" ; then
changeNamesWithRecursion ${args[0]};
else
#there is an argument and not a path
echo "error - give path not sed";
exit;
fi
fi
}
function preformSed { #no paramiters
if [[ ${#args[*]} == 0 ]]; then
#no parameters and no arguments
echo $help;
exit;
fi
if [[ ${#args[*]} == 1 ]]; then #need to perform sed pattern in current directory
if test "${args[1]+isset}" ; then
echo "awegwegn: ${args[*]}";
changeSedNames $PWD ${args[1]};
else
echo "error - give sed pattern not ${args[0]}";
exit;
fi
fi
if [[ ${#args[*]} == 2 ]]; then #need sed pattern and path to perform
changeSedNames ${args[0]} ${args[1]};
fi
}
function preformSedwithRecursion { #no paramiters
if [[ ${#args[*]} == 0 ]]; then
#no parameters and no arguments
echo $help;
exit;
fi
if [[ ${#args[*]} == 1 ]]; then #need to perform sed pattern in current directory
if test "${args[1]+isset}" ; then
changeSedNamesWithRecursion $PWD ${args[1]};
else
echo "error - give sed pattern not ${args[0]}";
exit;
fi
fi
if [[ ${#args[*]} == 2 ]]; then #need sed pattern and path to perform
changeSedNamesWithRecursion ${args[0]} ${args[1]};
fi
}
# --------------------------------------------------------------------INIT
#get data
args=(); #arguments -> args( path sed )
param=( false false ); #paramiters
#get paramiters
while getopts "hrlu" options; do
case "${options}" in
\?)
echo "Invalid parameter: -$OPTARG. Use -r -l -u or own pattern" >&2
exit
;;
h)
echo $help
exit
;;
r)
param[0]='r';
;;
l)
param[1]='l';
;;
u)
param[1]='u';
;;
esac
done
all_args=($*);
getArguments;
# echo "arguments: ${args[*]}";
# echo "parameters: ${param[*]}";
# echo "all args: ${all_args[*]}";
#main part
if [ ${param[0]} == false ] && [ ${param[1]} == false ]; then
#no parameters
preformSed;
elif [ ${param[0]} != false ] && [ ${param[1]} != false ]; then
#two parameters
performOptionWithRecursion;
else
#one paramiter
if [ ${param[0]} == r ]; then
#recursion
preformSedwithRecursion;
else
#option
performOption;
fi
fi