-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmamu.sh
326 lines (283 loc) · 8.54 KB
/
mamu.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
#!/bin/sh
#!/usr/bin/env bash
#set -v
COLOR_NONE=`tput sgr0` # No Color
COLOR_RED=`tput setaf 1`
COLOR_GREEN=`tput setaf 2`
COLOR_YELLOW=`tput setaf 3`
COLOR_BLUE=`tput setaf 4`
COLOR_MAGENTA=`tput setaf 5`
COLOR_CYAN=`tput setaf 6`
COLOR_WHITE=`tput setaf 7`
TEXT_BOLD=`tput bold`
TEXT_NORMAL=`tput sgr0`
########### private utility methods ############
contains() {
string="$1"
substring="$2"
if test "${string#*$substring}" != "$string"
then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
printTipsText()
{
echo "${COLOR_CYAN}${TEXT_BOLD}::TIPS:: $1 ${COLOR_NONE}"
}
printSuccessText()
{
echo "${COLOR_GREEN}${TEXT_BOLD}Success: $1 ${COLOR_NONE}"
}
printFailureText()
{
echo "${COLOR_RED}${TEXT_BOLD}Failure: $1 ${COLOR_NONE}"
}
folderExists(){
if [ -d "$1" ]; then
return 1;
fi
return 0;
}
fileExists(){
if [ -f "$1" ]; then
return 1;
fi
return 0;
}
readInputFile()
{
until [ "$FILE_PATH" ]; do
read -e -p "${COLOR_BLUE}$1 ${COLOR_NONE}" FILE_PATH
done
echo $(abspath "$FILE_PATH")
}
readText()
{
until [ "$INPUT_TEXT" ]; do
read -p "${COLOR_BLUE} $1 ${COLOR_NONE}" INPUT_TEXT
done
echo $INPUT_TEXT
}
ltrim() #{{{1 # Removes all leading whitespace (from the left).
{
local char=${1:-[:space:]}
sed "s%^[${char//%/\\%}]*%%"
}
rtrim() #{{{1 # Removes all trailing whitespace (from the right).
{
local char=${1:-[:space:]}
sed "s%[${char//%/\\%}]*$%%"
}
trim() #{{{1 # Removes all leading/trailing whitespace
{
#could've been very simple, like TRIMMED_PATH=`echo "$SOME_PATH" | sed 's/^ *//;s/ *$//'`
ltrim "$1" | rtrim "$1"
}
squeeze() #{{{1 # Removes leading/trailing whitespace and condenses all other consecutive whitespace into a single space.
{
local char=${1:-[[:space:]]}
sed "s%\(${char//%/\\%}\)\+%\1%g" | trim "$char"
}
abspath() #{{{1 # Gets the absolute path of the given path. Will resolve paths that contain '.' and '..'. Think readlink without the symlink resolution.
{
local path=${1:-$PWD}
# Path looks like: ~user/...
# Gods of bash, forgive me for using eval
if [[ $path =~ ~[a-zA-Z] ]]; then
if [[ ${path%%/*} =~ ^~[[:alpha:]_][[:alnum:]_]*$ ]]; then
path=$(eval echo $path)
fi
fi
# Path looks like: ~/...
[[ $path == ~* ]] && path=${path/\~/$HOME}
# Path is not absolute
[[ $path != /* ]] && path=$PWD/$path
path=$(squeeze "/" <<<"$path")
local elms=()
local elm
local OIFS=$IFS; IFS="/"
for elm in $path; do
IFS=$OIFS
[[ $elm == . ]] && continue
if [[ $elm == .. ]]; then
elms=("${elms[@]:0:$((${#elms[@]}-1))}")
else
elms=("${elms[@]}" "$elm")
fi
done
IFS="/"
echo "/${elms[*]}"
IFS=$OIFS
}
#helpText#$ mamu help (Prints help for all available commands)
help()
{
echo "${COLOR_MAGENTA}------------------------------ List of utilities -------------------------------\n"
sed -n 's/^#helpText#//p' $0
echo "\n------------------------------------ END ---------------------------------------\n${COLOR_NONE}"
}
################################################################################
####################### Utility Commands functions #############################
################################################################################
#helpText#$ mamu countdown (counts down with voice feedback a certain amount of seconds)
countdown() {
SECONDS=0
if [ $# -lt 1 ]
then
printTipsText "You can also use countdown command like - $ mamu countdown 10 "
SECONDS=$(readText "How many seconds you want to countdown? -> ")
else
SECONDS=$1
fi
for (( i=SECONDS; i>0; i--)); do
sleep 1 &
printf " $i \r"
say $i
wait
done
say 'countdown done'
}
#helpText#$ mamu findnreplace (finds given text and replaces it with a new one in a file)
findnreplace(){
FILE_NAME=""
FIND_TEXT=""
REPLACE_TEXT=""
if [ $# -lt 3 ]
then
printTipsText "You can also use findnreplace command like - $ mamu findnreplace /Desktop/myFile.txt 'Old text' 'New text'"
FILE_NAME=$(readInputFile "In which file you want to find a replace text? -> ")
FIND_TEXT=$(readText "Which text you want to find? -> ")
REPLACE_TEXT=$(readText "What text you want to replace with? -> ")
else
FILE_NAME=$1
FIND_TEXT=$2
REPLACE_TEXT=$3
fi
##remove the white spaces
FILE_NAME=`echo "$FILE_NAME" | sed 's/^ *//;s/ *$//'`
sed -i.bak "s/$FIND_TEXT/$REPLACE_TEXT/g" $FILE_NAME
DIRECTORY_NAME=$(dirname "$FILE_NAME")
rm $DIRECTORY_NAME//*".bak"
}
#helpText#$ mamu findtext (finds given text in a file or folder and shows a list of them)
findtext()
{
SEARCH_TEXT=""
SEARCH_PATH=""
if [ $# -lt 2 ]
then
printTipsText "You can also use findtext command like - $ mamu findtext 'love' ~/Desktop/Song-Lyrics"
SEARCH_PATH=$(readInputFile "Which file/folder you want to search? ${COLOR_NONE}->")
SEARCH_TEXT=$(readText "What text you want to search in files? -> " )
else
SEARCH_PATH=$(abspath "$1")
SEARCH_TEXT="$2"
fi
grep -irw -n $SEARCH_PATH -e "$SEARCH_TEXT"
#grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
#grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
}
#helpText#$ mamu symboliclink (make a symbolic link of a file/folder into another folder)
symboliclink()
{
SOURCE_PATH=""
DESTINATION_PATH=""
if [ $# -lt 2 ]
then
printTipsText "You can also use symboliclink command like - $ mamu symboliclink ~/Desktop/Songs/MichelJackson ~/Desktop/FavouriteSongs"
SOURCE_PATH=$(readInputFile "Which folder/file you want to make symbolic-link of? -> " )
DESTINATION_PATH=$(readInputFile "Where to put the symbolic-link? -> ")
else
SOURCE_PATH=$(abspath "$1")
DESTINATION_PATH=$(abspath "$2")
fi
ln -s $SOURCE_PATH $DESTINATION_PATH
if [ $? -eq 0 ]; then
printSuccessText "Made a symbolic link from $SOURCE_PATH to $DESTINATION_PATH"
fi
}
#helpText#$ mamu httpserver (starts basic python HTTP server in a given port & directory)
httpserver()
{
SOURCE_PATH=""
SERVER_PORT=""
if [ $# -ne 2 ]; then
printTipsText "You can also use findnreplace command like - $ mamu httpserver ~/Desktop/MyWebsite 8080"
SOURCE_PATH=$(readInputFile "Which folder you want to serve as HTTP server? -> " )
SERVER_PORT=$(readText "What port to use for serving HTTP server? (ex: 8080) -> " )
pushd "$SOURCE_PATH"; python -m SimpleHTTPServer $SERVER_PORT; popd
else
pushd "$1"; python -m SimpleHTTPServer $2; popd
fi
}
#helpText#$ mamu killnode (kills all running node.js instances or a given one)
killnode(){
if [ $# -ne 1 ]; then
printTipsText "You can also use findnreplace command like - $ mamu killnode app.js"
kill $(ps aux | grep .js | awk '{print $2}')
if [ $? -eq 0 ]; then
printSuccessText "Killed all node process!"
fi
else
PROCESS_NAME="$1"
kill $(ps aux | grep $PROCESS_NAME | awk '{print $2}')
if [ $? -eq 0 ]; then
printSuccessText "Killed $PROCESS_NAME node process!"
fi
fi
}
#helpText#$ mamu testtls (tests which TLS versions supported on a given website)
testtls()
{
SERVER=""
if [ $# -ne 1 ]; then
printTipsText "You can also use testtls command like - $ mamu testtls https://google.com"
SERVER=$(readText "What HTTP/S server you want to test? -> " )
else
SERVER="$1"
fi
function testTLSVersion()
{
TLS=$1
echo "Testing TLS$1 on $SERVER..."
OUT=$(curl -v --silent --tlsv$TLS https://$SERVER/ 2>&1 | grep TLS)
if [ -z "$OUT" ]; then
printFailureText "TLS$TLS is NOT SUPPORTED on $SERVER"
else
printSuccessText "TLS$TLS is supported on $SERVER"
fi
}
testTLSVersion 1.2
testTLSVersion 1.1
testTLSVersion 1.0
}
#helpText#$ mamu iossimulator ( shows/open ios simulator documents directory location)
iossimulator()
{
BUNDLEID=""
if [ $# -ne 1 ]; then
printTipsText "You can also use iossimulator command like - $ mamu iossimulator com.mycompany.myapp"
BUNDLEID=$(readText "What is the bundle identifier of the app? -> " )
else
BUNDLEID="$1"
fi
open `xcrun simctl get_app_container booted $BUNDLEID data`
PATH=$(xcrun simctl get_app_container booted $BUNDLEID data)
printSuccessText "Simulator documents directory Path for $BUNDLEID is: $PATH"
}
########################################
####### ENTRY POINT OF USER INPUT ######
########################################
if [ $# -lt 1 ]; then
echo "${COLOR_CYAN}${TEXT_BOLD}\nWelcome to ShellMamu, a collection of shell utilities which ask for arguments. ${COLOR_NONE}"
help
else
"$@"
if [ $? -ne 0 ]; then
echo "${COLOR_RED}${TEXT_BOLD}Please provide a valid utility name, available utilities are: ${COLOR_NONE}"
help
fi
fi
exit