-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash_function
355 lines (325 loc) · 7.12 KB
/
bash_function
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/bin/bash
## chain command
mcd() {
mkdir $@
cd $1
}
cls() {
cd $1
ls
}
err() {
echo "$@" >&2
}
gvfs() {
gvfs-$*
}
## change and execute command
big5() {
iconv -f big5 "$@"
}
jp() {
iconv -f CP932 "$@"
}
cn() {
iconv -f GB2312 "$@"
}
value() {
local exit_state
case "$1" in
-q )
shift
quiet "$@"
;;
-Q )
shift
quiet -Q "$@"
;;
* )
"$@"
;;
esac
exit_state=$?
echo $exit_state
return $exit_state
}
quiet() {
if [ "$1" = -Q ]
then
shift
"$@" >/dev/null 2>&1
else
"$@" >/dev/null
fi
}
as_return() {
return $(($1 == 0))
}
set_if_empty() {
local name value
name=$1
value="$2"
if eval [ -z \"\$$name\" ]
then
eval $name='"$value"'
return 0
else
return 1
fi
}
nth() {
local n=$1
shift $((n+1))
echo "$1"
}
cutdf() {
local delimeter="$1"
local field="$2"
shift
shift
cut -d "$delimeter" -f "$field" "$@"
}
ssed() {
local string="$1"
shift
echo "$string" | sed "$@"
}
tcat() {
local option
if [ $# -eq 1 ]
then option=tf
else option=xOf
fi
tar -$option "$@"
}
ehost() {
{
getent ahosts "$@"
} || {
err getent ahost not found, try host
host "$@"
} || {
err host not found, try ssh/config
grep -A 1 "$@" $HOME/.ssh/config
}
}
psparent() {
local pid=$1
[ -z "$pid" ] && pid=$$
echo $pid
while [ $pid -gt 1 ]
do
pid=$(command ps -o ppid:1 --no-headers $pid)
echo $pid
done
}
fin() {
local name="$1"
shift
find "$@" -iname "*$name*"
}
errwhy() {
local status=$? signal
if [ $# -eq 1 ]
then status=$1
fi
case $status in
126) echo BASH $status file is not executable ;;
127) echo BASH $status command not found ;;
0) echo SIGEXIT $status nothing wrong ;;
'')
echo no exit code provide
return 5
;;
*)
if [ $status -gt 127 ]
then
signal=$(kill -l $((127 & $status)))
# echo $signal
# echo 'look up by `man signal.7`' /SIG$signal
local message="SIG$signal $((127 & status)) cause exit $status"
man --prompt="$message" \
--pager="less -j 4 -p SIG$signal -p Standard.signals" \
signal.7
echo $message
elif ! [ $status -lt 127 ]
then
err exit code should between 0-255
return 22
fi
;;
esac
errno $status
}
encfsr() {
# convert relative path to absolute path for encfs
i=0
local new_arg
while [ $i -lt $# ]
do
if [ -d "$1" ]
then new_arg="$(realpath "$1")"
else new_arg="$1"
fi
shift
set -- "$@" "$new_arg"
i=$((i+1))
done
encfs "$@"
}
acd() {
# acd - cd to avfs directory
# usage
# acd # switch bewteen $HOME/.avfs$PWD and $PWD
# acd directory # go to $HOME/.avfs/../directory
# acd archive.tar.gz # go to $HOME/.avfs/../archive.tar.gz#
# acd archive.rar urar # go to $HOME/.avfs/../archive.rar#urar
if ! [ $acd_avfs_mount ]
then
if mount | grep -q avfs
then acd_avfs_mount=t
else mountavfs && acd_avfs_mount=t
fi
fi
local base="$HOME/.avfs"
local length=`expr length "$base"`
local target="$1"
local parameter="$2"
local in_avfs=$(expr substr "$PWD" 1 \( $length + 1 \) = "$base/" )
local target_path_absolute=$(expr substr "$target" 1 1 = /)
if [ $# -eq 0 ] # switch between avfs and normal
then
if [ $in_avfs -eq 1 ]
then cd "$(expr substr "$PWD" \( $length + 1 \) \( length "$PWD" \))"
else cd "$base$PWD"
fi
elif [ -d "$target" ] && [ $# -eq 1 ] # go to target path in avfs
then
if [ $target_path_absolute -eq 1 ]
then cd "$base$target"
else cd "$base$PWD/$target"
fi
elif [ -d "$target#$parameter" ] # if target already exist as avfs, just go
then cd "$target#$parameter"
else # target is file
if [ $target_path_absolute -eq 1 ]
then cd "$base$target#$parameter"
else cd "$base$PWD/$target#$parameter"
fi
fi
}
fd_is_open() {
local fd=$1
( true >&$fd ) 2>/dev/null
}
fd_first_free() {
local fd
if [ $# -gt 0 ]
then fd=$1
else fd=0
fi
while fd_is_open $fd
do fd=$((fd+1))
done
echo $fd
}
# fc for script file
fx() {
local command=$1
shift
history -s fx $command
local temp_script
if [ -t 1 ]
then temp_script=$(mktemp /tmp/fx.XXXXXXXX.tmp)
fi
(
if [ -n "$temp_script" ]
then exec >$temp_script
fi
if [ -f "$command" ]
then
echo \(
echo "set -- $*"
echo "unalias -a"
cat "$command"
echo \)
elif [ -z "$command" ]
then
clipboard
else
case `type -t $command` in
file)
echo \(
echo "set -- $*"
echo "unalias -a"
cat `type -P $command`
echo \)
;;
"function")
declare -f $command
if [ $# -gt 0 ]
then echo $command $*
fi
;;
"alias")
alias $command
if [ $# -gt 0 ]
then echo $command $*
fi
;;
"")
err command not found: $command
return 3
;;
*)
err not editing support command type: `type -t $command`
return 38
;;
esac
fi
)
local exit_code=$?
if [ -n "$temp_script" ]
then
if [ $exit_code -eq 0 ]
then
$EDITOR $temp_script
history -s "$(cat $temp_script)"
cat $temp_script >&2
. $temp_script
fi
command rm $temp_script
fi
return $exit_code
}
alias cd=cd_gholk
cd_gholk() {
if [ $# = 0 ]
then
pushd ~ >/dev/null
return
fi
case x$1 in
x-) pushd ;;
x-c) dirs -c ;;
x-l) dirs ;;
x-*)
if [ 0 -gt $1 ] > /dev/null 2>&1
then
local positive=$(($1 * -1))
pushd "$(dirs -l +$positive)" > /dev/null
popd +$((positive+1))
else
echo err unknown option "$@"
fi
;;
*) pushd "$@" ;;
esac
}
source_function_file() {
# usage: alias fx='source_function_file $HOME/.local/share/loco/fx.sh'
# or alias $name="source_function_file $name"
local function_file=$1
shift
. $HOME/.local/lib/function-file/$function_file.sh
}