-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch
executable file
·126 lines (107 loc) · 2.62 KB
/
search
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
#!/bin/bash
# version: 0.3
# author: github.com/mauriaguilar
# CONFIGURATION
this_file="search" # name of this file
list_file="list.txt"
alias_file="$HOME/.$this_file"
# UNINSTALL COMMAND "u"
if [ ""$1 == "u" ] || [ ""$1 == "-u" ]
then
echo "Uninstalling... :("
rm $alias_file 2> /dev/null
# remove alias in ~/.bashrc. Not implemented.
exit
fi
# INSTALLATION OF ALIAS
if [ -r $alias_file ] && [ -s $alias_file ]
then
# The file ~/.$alias_file exists.
# If there are no parameters, exit.
if [ -z $1 ]
then
echo "Nothing to do."
exit
fi
else
echo "The "$alias_file" file does not exists. Installing "$this_file" alias in ~/.bashrc :)"
echo 'alias '$this_file'='$alias_file >> ~/.bashrc
cp $this_file $alias_file
chmod u+r+w+x $alias_file
exit
fi
# HELP COMMAND: "h"
if [ $1 == "h" ] || [ $1 == "-h" ]
then
echo "Use: "$this_file" term1 ... termN [u | h] [k p]"
echo "Search one or multiple terms in the files in this folder, recursively, one at a time."
echo " How works:"
echo " Recursively search the files in this folder that contain all the terms. The results go to "$list_file"."
echo " In the next search, look over the files of the previous result. The results go to "$list_file" again."
echo " parameters:"
echo " u, -u Uninstall alias. "
echo " k, -k Keep previous result and search in the files of the "$list_file" file. (optional). "
echo " p, -p Print all results. (optional). "
echo " h, -h Print this help."
exit
fi
# CHECK TERMS TO SEARCH
function check_terms()
{
if [ $2 ]
then
# Shift optional parameter to search.
shift
else
echo "Nothing to search."
exit
fi
}
# OPTIONAL KEEP COMMAND: "r"
if [ $1 == "k" ] || [ $1 == "-k" ]
then
echo "Keeping previous result."
check_terms
else
rm $list_file 2> /dev/null
fi
# OPTIONAL PRINT COMMAND: "p"
PRINT_ALL_RESULTS=""
if [ $1 == "p" ] || [ $1 == "-p" ]
then
PRINT_ALL_RESULTS=true
check_terms
fi
# SINGLE SEARCH FUNCTION
function search()
{
if [ -r $list_file ] && [ -s $list_file ]
then
#echo "Searching again. (The "$list_file" file exists)"
grep -e $1 $(cat $list_file) -l > $list_file
else
#echo "Searching for the first time. (The "$list_file" file does not exist or is empty)"
grep -e $1 -r ./ -l > $list_file
fi
if [ $PRINT_ALL_RESULTS ]
then
cat $list_file
fi
}
# RECURSIVE SEARCH
while [ $1 ]
do
if [ $PRINT_ALL_RESULTS ]
then
echo "------------------------------"
echo " Searching "\"$1\"
echo "------------------------------"
fi
search $1
shift
done
# Print final result
if [ -z $PRINT_ALL_RESULTS ]
then
cat $list_file
fi