-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.bash
200 lines (142 loc) · 3.6 KB
/
string.bash
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
#!/usr/bin/env bash
: <<'!COMMENT'
GGCOM - Bash - Library - Strings v201504251127
Louis T. Getterman IV (@LTGIV)
www.GotGetLLC.com | www.opensour.cc/ggcom/bash/lib/string
Example usage:
source "string.bash"
echo `str_repeat - 80`
Thanks:
http://www.linuxjournal.com/content/return-values-bash-functions
http://stackoverflow.com/questions/5349718/how-can-i-repeat-a-character-in-bash
http://forums.gentoo.org/viewtopic-t-660965-view-previous.html
http://stackoverflow.com/questions/13210880/replace-one-substring-for-another-string-in-shell-script
To-do:
* Add count into str_replace
!COMMENT
function strtoupper() {
local input="$1"
echo "$input" | tr '[:lower:]' '[:upper:]'
} # END FUNCTION: strtoupper
function strtolower() {
local input="$1"
echo "$input" | tr '[:upper:]' '[:lower:]'
} # END FUNCTION: strtolower
function substr() {
local input="$1"
local start=${2-0}
local stop=${3-''}
if [ ! -z $stop ]; then
echo "${input:$start:$stop}"
else
echo "${input:$start}"
fi
} # END FUNCTION: substr
# echo `str_repeat - 80`
function str_repeat() {
local char=$1
local repeat=$2
local stat="`seq -s${char} ${repeat} | tr -d '[:digit:]'`"
echo $stat
} # END FUNCTION: str_repeat
function str_replace() {
#----- Variables
local search="$1"
local replace="$2"
local subject="$3"
local count='' # use / to replace one or // to replace all
#----- Variables
echo "${subject//$search/$replace}"
} # END FUNCTION: str_replace
function parseUserHost() {
#----- Variables
local raw=$1
local choice=$2
local user="${raw%@*}" # BEFORE @
local rawhost="${raw#*@}" # AFTER @
local host="${rawhost%:*}" # BEFORE :
local path="${rawhost#*:}" # AFTER :
#-----/Variables
case $choice in
user)
ret=$user
;;
host)
ret=$host
;;
path)
if [ "$path" != "$host" ]; then
ret=$path
else
ret=''
fi
;;
*)
ret=$raw
;;
esac
echo "$ret"
} # END FUNCTION: parseUserHost
function validateUserHost() {
#----- Variables
local qstnDestStrTest="$1"
#-----/Variables
# Attempt to parse variables from argument
if [ ! -z "$qstnDestStrTest" ]; then
ansrUser=`parseUserHost "$qstnDestStrTest" user`
ansrSrvr=`parseUserHost "$qstnDestStrTest" host`
ansrDest=`parseUserHost "$qstnDestStrTest" path`
# Implicit localhost, trigger localhost and user test below
if [ "$ansrSrvr" == 'localhost' ]; then ansrSrvr="$ansrUser"; fi
# validUser@localhost
if [ "$ansrUser" == "$ansrSrvr" ] && [ "$(eval echo "~$ansrUser")" != "~${ansrUser}" ]; then
ansrSrvr="localhost"
# invalidUser@localhost
elif [ "$ansrUser" == "$ansrSrvr" ]; then
ansrUser=''
fi
fi # END
# Build output value
if [ ! -z "$ansrUser" ]; then
# user@host
qstnDestStrTest="${ansrUser}@${ansrSrvr}"
# Add destination
if [ ! -z "$ansrDest" ]; then
qstnDestStrTest="${qstnDestStrTest}:`mod_trail_slash add "$ansrDest"`"
fi
# Invalid
else
qstnDestStrTest=''
fi
# Return value
echo "$qstnDestStrTest"
} # END FUNCTION: validateUserHost
function mod_trail_slash() {
#----- Variables
local action="$1"
local path_raw="$2"
local ret=''
#-----/Variables
case "$action" in
"add")
ret="$path_raw"
if [ "${ret: -1}" != '/' ]; then ret="${ret}/"; fi # Add trailing slash
;;
"rem")
ret="$path_raw"
if [ "${ret: -1}" == '/' ]; then ret="${ret%?}"; fi
;;
*)
ret=''
;;
esac
echo "$ret"
} # END FUNCTION: mod_trail_slash
function isolate_dir_name() {
#----- Variables
local path_raw="$1"
#----- Variables
path_raw=`mod_trail_slash rem "$path_raw"`
path_raw="${path_raw##*/}"
echo "$path_raw"
} # END FUNCTION: isolate_dir_name