-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinks.sh
143 lines (126 loc) · 3 KB
/
links.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
#!/usr/bin/env bash
source "$HOME/.linkss/.links"
showHelp() {
echo """
linkss()
NAME
linkss -
DESCRIPTION
Command Line Tool to add urls as shell command
SYNOPSIS
linkss [options]
OPTIONS
-h, --help
show help
-l --list
list existing commands
-a --add
add/modify existing commands
-d --delete
delete existing command
"""
}
showList () {
emptyFile
if [ "$?" -eq 0 ]; then
return
fi
while read line; do
echo "$line" | awk -F',' '{print $2 " --> " $1}'
done <"$HOME/.linkss/links.txt"
}
function emptyFile() {
if [ ! -s "$HOME/.linkss/links.txt" ]; then
echo "no saved urls"
return 0
fi
return 1
}
delLink() {
emptyFile
if [ "$?" -eq 0 ]; then
return
fi
read -p "Enter url : " url
while [[ "$url" == "" ]]
do
read -p "Enter url : " url
done
grep -v "^$url," "$HOME/.linkss/links.txt" > _tempLinks
mv _tempLinks "$HOME/.linkss/links.txt"
grep -v "{ open \"$url\"; }$" "$HOME/.linkss/.links" > _tempCommands
mv _tempCommands "$HOME/.linkss/.links"
echo "url $url removed successfully"
}
linkss () {
if [ "$#" -ne 1 ]; then
showHelp
return
else
case "$1" in
-h|--help)
showHelp
return
;;
-l|--list)
showList
return
;;
-a|--add)
;;
-d|--delete)
delLink
return
;;
*)
echo "unsupported option $1"
showHelp
return
;;
esac
fi
linksTxtPath="$HOME/.linkss/links.txt"
linksCommandsPath="$HOME/.linkss/.links"
echo "Enter url and short name"
read -p "Enter url : " url
while [[ "$url" == "" ]]
do
read -p "Enter url : " url
done
inpuSuffix=""
currsName=""
matchLine=`grep "^$url," "$linksTxtPath"`
if [[ "$matchLine" != "" ]]
then
currsName=`echo $matchLine | awk -F"," '{print $2}'`
inpuSuffix="[current=$currsName] "
fi
read -p "Enter short name for url $inpuSuffix: " sName
while [[ "$sName" == "" ]] && [[ "$currsName" == "" ]]
do
read -p "Enter short name for url $inpuSuffix: " sName
done
# previous name & current name are different
if [[ "$sName" != "$currsName" ]]
then
newLine="$url,$sName"
# previous name is not empty
if [[ "$currsName" != "" ]]
then
# no new name provided
if [[ "$sName" == "" ]]; then
return
fi
grep -v "^$url," "$linksTxtPath" > _tempLinks
mv _tempLinks "$linksTxtPath"
grep -v "^$currsName ()" "$linksCommandsPath" > _tempCommands
mv _tempCommands "$linksCommandsPath"
action="updated"
else
action="added"
fi
echo "$newLine" >> "$linksTxtPath"
echo "$sName () { open \"$url\"; }" >> "$linksCommandsPath"
echo "for url $url command $sName $action successfully"
fi
}