-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bash-lite.sh
executable file
·696 lines (674 loc) · 22.4 KB
/
bash-lite.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# # /usr/bin/env bash
# -*- mode: bash; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: t-*-
# vi: set ft=bash noet ci pi sts=0 sw=2 ts=2:
# st:
#
#
# # /usr/bin/env bash
#
# bash.sh:
# Standard Template for bash/zsh developing.
# Version: v20241112
# License: MIT
# Site: https://github/hedzr/bash.sh
#
# Usages:
# $ ./bash.sh cool
# $ DEBUG=1 ./bash.sh
#
# $ ./bash.sh debug-info
#
# $ ./bash.sh 'is_root && echo Y'
# $ sudo ./bash.sh 'is_root && echo Y'
# $ sudo DEBUG=1 ./bash.sh 'is_root && echo y'
#
# $ HAS_END=: ./bash.sh
# $ HAS_END=false ./bash.sh
#
# Use installer:
# $ curl -sSL https://hedzr.com/bash.sh/installer | sudo bash -s
#
# erase this function and start yours
debug_info() {
debug_begin
cat <<-EOF
in_debug: $(in_debug && echo Y || echo '.')
is_root: $(is_root && echo Y || echo '.')
is_bash: $(is_bash && echo Y || echo '.') # STRICTED = $(is_bash_strict && echo Y || echo N), SHELL = $SHELL, BASH_VERSION = $BASH_VERSION
is_zsh/is_zsh_t1: $(is_zsh && echo Y || echo '.') / $(is_zsh_t1 && echo Y || echo '.') # $(is_zsh && echo "ZSH_EVAL_CONTEXT = $ZSH_EVAL_CONTEXT, ZSH_NAME/VERSION = $ZSH_NAME v$ZSH_VERSION" || :)
is_fish: $(is_fish && echo Y || echo '.') # FISH_VERSION = $FISH_VERSION
in_sourcing: $(in_sourcing && echo Y || echo '.')
if_vagrant/in_vm: $(if_vagrant && echo Y || echo '.') / $(in_vm && echo Y || echo '.')
in_vscode: $(in_vscode && echo Y || echo '.')
in_jetbrains: $(in_jetbrains && echo Y || echo '.')
in_vim/neovim: $(in_vim && echo Y || echo '.') / $(in_neovim && echo Y || echo '.')
darwin/linux/win(wsl): $(is_darwin && echo Y || echo '.')$(is_darwin_sillicon && echo ' [Sillicon] ' || echo ' ')/ $(is_linux && echo Y || echo '.') / $(is_win && echo Y || echo '.')
is_interactive_shell: $(is_interactive_shell && echo Y || echo '.')
NOTE: bash.sh can only work in bash/zsh mode, even if running it in fish shell.
IP(s): 4 -> $(lanip | join_lines), 6 -> $(lanip6 | join_lines)
$(lanipall | pad 9)
Gateway / Mask: $(gw) / $(netmask)
Subnet Prefix: $(subnet4)
EOF
fn_exists pmid && cat <<-EOF
OS tests: pmid='$(pmid)' osid='$(osid)' osidlike='$(osidlike)'
oscodename='$(oscodename)' versionid='$(versionid)' variantid='$(variantid)'
if_nix_typ='$(if_nix_typ)' (\$OSTYPE='$OSTYPE')
is_suse_series='$(is_suse_series && echo Y || echo .)'
EOF
is_linux && cat <<-EOF
lsb_release_cs = '$(lsb_release_cs)'
uname_kernel(-s) = '$(uname_kernel)'
uname_cpu(-p) = '$(uname_cpu)'
uname_mach(-m) = '$(uname_mach)'
uname_rev(-r) = '$(uname_rev)'
uname_ver(-v) = '$(uname_ver)'
i386_amd64 = '$(i386_amd64)'
x86_64 = '$(x86_64)'
if_hosttype = '$(if_hosttype)'
EOF
debug_end
:
}
slepp() { :; }
#### write your functions here, and invoke them by: `./bash-lite.sh <your-func-name>`
cool() { echo cool; }
sleeping() { echo sleeping; }
_my_main_do_sth() {
local xcmd cmd=${1:-sleeping} && { [[ $# -ge 1 ]] && shift; } || :
# for linux only:
# local cmd=${1:-sleeping} && shift || :
# eval "$cmd $@" || :
fn_exists "$cmd" && {
debug "$cmd - $@"
eval $cmd "$@"
} || {
xcmd="lite-$cmd" && fn_exists "$xcmd" && eval $xcmd "$@" || {
xcmd="try-lite-$cmd" && fn_exists "$xcmd" && eval $xcmd "$@" || {
xcmd="build-c$cmd" && fn_exists "$xcmd" && eval $xcmd "$@"
}
}
}
unset cmd xcmd
}
########################################################
#### HZ Tail BEGIN #### v20241112 ####
in_debug() { (($DEBUG)); }
in_provisioning() { (($PROVISIONING)); } ## return exit status as true if $PROVISIONING is not equal to 0
is_root() { [ "$(id -u)" = "0" ]; }
is_bash() { is_bash_t1 || is_bash_t2; }
is_bash_t1() { [ -n "$BASH_VERSION" ]; }
is_bash_t2() { [ ! -n "$BASH" ]; }
is_bash_strict() { if is_bash; then if is_zsh_strict; then false; else true; fi; else false; fi; }
is_zsh() { [[ -n "$ZSH_NAME" || "$SHELL" = */zsh ]]; }
is_zsh_strict() { [[ -n "$ZSH_NAME" && "$SHELL" = */zsh ]]; }
is_zsh_t1() { [[ "$SHELL" = */zsh ]]; }
is_zsh_t2() { [ -n "$ZSH_NAME" ]; }
is_fish() { [ -n "$FISH_VERSION" ]; }
is_darwin() { [[ $OSTYPE == darwin* ]]; }
is_darwin_sillicon() { is_darwin && [[ $(uname_mach) == arm64 ]]; }
is_linux() { [[ $OSTYPE == linux* ]]; }
is_win() { in_wsl; }
in_wsl() { [[ "$(uname -r)" == *windows_standard* ]]; }
in_sourcing() {
# https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced
if is_zsh; then
[[ "$ZSH_EVAL_CONTEXT" == *:file:* ]]
else
[[ $(basename -- "$0") != $(basename -- "${BASH_SOURCE[0]}") ]]
fi
}
in_vscode() { [[ "$TERM_PROGRAM" == "vscode" ]]; } # or VSCODE_INJECTION=1
in_jetbrains() { [[ "$TERMINAL_EMULATOR" == *JetBrains* ]]; }
in_vim() { [[ "$VIM" != "" ]] && [[ "$VIMRUNTIME" != "" ]]; }
in_neovim() { [[ "$NVIM" != "" ]] || [[ "$NVIM_LOG_FILE" != "" ]] || [[ "$NVIM_LISTEN_ADDRESS" != "" ]]; }
is_interactive_shell() { [[ $- == *i* ]]; }
is_not_interactive_shell() { [[ $- != *i* ]]; }
is_ps1() { [ -z "$PS1" ]; }
is_not_ps1() { [ ! -z "$PS1" ]; }
# The [ -t 1 ] check only works when the function is not called from
# a subshell (like in `$(...)` or `(...)`, so this hack redefines the
# function at the top level to always return false when stdout is not
# a tty.
if [ -t 1 ]; then
alias is_stdin=true
alias is_not_stdin=false
alias is_ttya=true
else
alias is_stdin=false
alias is_not_stdin=true
alias is_ttya=false
fi
cmd_exists() { command -v $1 >/dev/null; } # it detects any builtin or external commands, aliases, and any functions
fn_exists() { LC_ALL=C type $1 2>/dev/null | grep -qE '(shell function)|(a function)'; }
fn_builtin_exists() { LC_ALL=C type $1 2>/dev/null | grep -q 'shell builtin'; }
fn_aliased_exists() { LC_ALL=C type $1 2>/dev/null | grep -qE '(alias for)|(aliased to)'; }
fn_name() {
is_zsh && local fn_="${funcstack[2]}"
if [ "$fn_" = "" ]; then
is_bash && echo "${FUNCNAME[1]}"
else
echo "$fn_"
fi
# is_zsh && echo "${funcstack[2]}" || {
# is_bash && echo "${FUNCNAME[1]}"
# }
}
currentShell=
fn_name_dyn() {
if is_darwin; then
is_zsh && local fn_="${funcstack[2]}"
if [ "$fn_" = "" ]; then
is_bash && echo "${FUNCNAME[1]}"
else
echo "$fn_"
fi
return
fi
# local currentShell=$(ps -p $$ | awk "NR==2" | awk '{ print $4 }' | tr -d '-')
currentShell=${currentShell:-$(find_shell_by_pidtree)}
if [[ $currentShell == *'bash' ]]; then
echo ${FUNCNAME[1]}
elif [[ $currentShell == *'zsh' ]]; then
echo ${funcstack[2]}
else
echo "unknown func name ($currentShell)"
fi
}
#
#
if_nix_typ() {
case "$OSTYPE" in
*linux* | *hurd* | *msys* | *cygwin* | *sua* | *interix*) sys="gnu" ;;
*bsd* | *darwin*) sys="bsd" ;;
*sunos* | *solaris* | *indiana* | *illumos* | *smartos*) sys="sun" ;;
esac
echo "${sys}"
}
if_nix() { [[ "$(if_nix_typ)" == "$1" ]]; }
if_mac() { [[ $OSTYPE == darwin* ]]; }
if_ubuntu() {
if [[ $OSTYPE == linux* ]]; then
[ -f /etc/os-release ] && grep -qi 'ubuntu' /etc/os-release
fi
}
if_vagrant() { [ -d /vagrant ]; }
in_vagrant() { [ -d /vagrant ]; }
if_centos() {
if [[ $OSTYPE == linux* ]]; then
if [ -f /etc/centos-release ]; then
:
else
[ -f /etc/issue ] && grep -qEi '(centos|(Amazon Linux AMI))' /etc/issue
fi
fi
}
in_vm() {
if cmd_exists hostnamectl; then
# dbg "checking hostnamectl"
if hostnamectl | grep -iE 'chassis: ' | grep -q ' vm'; then
true
elif hostnamectl | grep -qE 'Virtualization: '; then
true
fi
else
# dbg "without hostnamectl"
false
fi
}
#
#
is_git_clean() { git diff-index --quiet "$@" HEAD -- 2>/dev/null; }
is_git_dirty() {
if is_git_clean "$@"; then
false
else
true
fi
}
git_clone() {
local Deep="--depth=1" Help Dryrun Https Dir arg i=1 Verbose=0
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
shift && Help=1
cat <<-EOT
git-clone helps cloneing git repo simply from github/gitlab/bitbucket
Usage: git-clone [-d|--deep] [-s|--https] [-o dir|--dir dir] repo
Description:
git-clone will pull the repo into 'user.repo/', for example:
git-clone hedzr/cmdr
GIT_HOST=gitlab.com git-clone hedzr/cmdr
git-clone [email protected]:hedzr/cmdr.git
git-clone https://github.com/hedzr/cmdr.git
will pull hedzr/cmdr into 'hedzr.cmdr/' directory.
Options and Args:
'--deep' enables full fetch, default is shallow pull only
'--https' enables https protocal, default is ssh protocol
'--dir' specifies the cloned target directory, default is 'user.repo'
'repo' can be these forms:
hedzr/cmdr
https://github.com/hedzr/cmdr
https://github.com/hedzr/cmdr.git
github.com:hedzr/cmdr.git
[email protected]:hedzr/cmdr.git
gitlab.com:hedzr/cmdr
bitbucket.com/hedzr/cmdr
git.sr.ht/hedzr/cmdr
gitee.com/hedzr/cmdr
coding.net/hedzr/cmdr
EnvVars:
GIT_HOSTS extras git hosts such as your own private host
GIT_HOST specify git host explicitly if you're using user/repo form.
EOT
;;
-d | --deep)
# strength=$OPTARG
shift && Deep=""
;;
-dr | --dry-run | --dryrun)
shift && Dryrun=1
;;
-s | --https)
shift && Https=1
;;
-o | --dir | --output)
shift && Dir="$1" && shift
;;
-v | --verbose)
Verbose=1 && shift
;;
*)
case $i in
1)
local Repo="${1:-hedzr/cmdr}"
shift
;;
esac
;;
esac
done
if [[ "$Help" != 1 ]]; then
local Sep='/' Prefix="${GIT_PREFIX:-git@}" Host="${GIT_HOST:-github.com}" h
[[ "$Https" -eq 1 ]] && Prefix="https://"
[[ "$Repo" =~ https://* ]] && Repo="${Repo//https:\/\//}"
for h in github.com gitlab.com bitbucket.com git.sr.ht gitee.com coding.net $GIT_HOSTS; do
[[ "$Repo" =~ $h/* ]] && Host=$h && Repo="${Repo//$h\//}"
[[ "$Repo" =~ $h:* ]] && Host=$h && Repo="${Repo//$h:/}"
done
Repo="${Repo%\#*}"
Repo="${Repo%\?*}"
Repo="${Repo#git@}"
Repo="${Repo%.git}"
Repo="${Repo%/blob/*}"
[[ "$Dir" == "" ]] && Dir="${Repo//\//.}"
[[ "$Prefix" == 'git@' ]] && Sep=':'
local Url="${Prefix}${Host}${Sep}${Repo}.git" Opts=""
(($Verbose)) && Opts="--verbose"
if [[ "$Dryrun" -ne 0 ]]; then
tip "Url: $Url | Deep?: '$Deep' | Opts: '$Opts'"
tip "Result: git clone $Deep -q $Opts "$Url" "$Dir""
else
dbg "cloning from $Url ..." && git clone $Deep -q $Opts "$Url" "$Dir" && {
(($Verbose)) && local DEBUG=1
dbg "git clone $Url DONE."
(($Verbose)) && du -sh "$Dir" || :
}
fi
fi
}
alias git-clone=git_clone
alias git-clone-deep='git_clone -d'
alias git-clone-deep-v='git_clone -d -v'
#
headline() { printf "\e[0;1m$@\e[0m:\n"; }
headline_begin() { printf "\e[0;1m"; } # for more color, see: shttps://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
headline_end() { printf "\e[0m:\n"; } # https://misc.flogisoft.com/bash/tip_colors_and_formatting
debug() { in_debug && printf "\e[0;38;2;133;133;133m$@\e[0m\n" || :; }
debug_begin() { printf "\e[0;38;2;133;133;133m"; }
debug_end() { printf "\e[0m\n"; }
dbg() { ((DEBUG)) && printf ">>> \e[0;38;2;133;133;133m$@\e[0m\n" || :; }
tip() { printf "\e[0;38;2;133;133;133m>>> $@\e[0m\n"; }
err() { printf "\e[0;33;1;133;133;133m>>> $@\e[0m\n" 1>&2; }
mvif() {
local src="$1" dstdir="$2"
if [ -d "$dstdir" ]; then
mv "$src" "$dstdir"
fi
}
#
#
join_lines() {
local delim="${1:-,}" ix=0
while read line; do
(($ix)) && printf '%s' "$delim"
let ix++
printf '%s' "$line"
done
}
strip_l() { echo ${1#"$2"}; }
strip_r() { echo ${1%"$2"}; }
pad() {
# pad 'pre', 'line' and 'post' as 3-column.
# the 1st arg is the count of indent spaces
# the 2nd and 3rd args are 'pre'-text and 'post'-text
# NOTE that the 'line' itself will be read from stdin.
# sample: cat 1.txt | pad 2
# or: find . -iname '*.log' -print -delete | pad 4 '' ' deleted.'
local line p=$1 && (($#)) && shift
local pre=$1 && (($#)) && shift
local post=$1 && (($#)) && shift
while read line; do printf '%-'$p"s%s%s%s\n" ' ' "$pre" "$line" "$post"; done # <<< "$@"
}
pad3() {
# pad 'pre', 'line' and 'post' as 3-column.
# the 1st arg is the count of indent spaces
# the 2nd arg is the width of 'line'
# the 3rd and 4th args are 'pre'-text and 'post'-text
# NOTE that the 'line' itself will be read from stdin.
# sample: ls -la | pad3 4 '-72' '' ' | desc here'
local line
local p=$1 && (($#)) && shift
local linewidth="${1:--1}" && (($#)) && shift
local pre=$1 && (($#)) && shift
local post=$1 && (($#)) && shift
while read line; do printf '%-'$p"s%s%${linewidth}s%s\n" ' ' "$pre" "$line" "$post"; done # <<< "$@"
}
rpad() {
# sample: rpad 32 - 'Some file' && echo '723 bytes'
# will got:
# Some file-----------------------723 bytes
local cnt=$1
local pad="$(char_repeat $2 $1)"
(($#)) && shift && (($#)) && shift && local y="$@"
# dbg "cnt: $cnt, pad: $pad, y: $y" 1>&2
y="${y:0:$cnt}${pad:0:$((cnt - ${#y}))}"
echo -n "$y"
}
char_repeat() {
# repeat char n times: `char_repeat '-' 32`
local pad=$1 && (($#)) && shift
local n="$1" && (($#)) && shift
printf '%*s' $n "" | tr ' ' "$pad"
}
safety() {
# safety make the folder name more safety when output. It
# replaces $HOME to '~' to prevent home user name leaked.
# In additions, it refers zsh tlide folder name list and
# do the replacements rely on it. That means, if you have
# a zsh hashed folder definition /usr/local/bin -> ~ulbin,
# then it can also be applied to the result of
# $(safety $string).
#
# For example,
#
# $ echo $(./bash.sh safety /home/$USER/Downloads)
# ~/Downloads
# $ echo $(./bash.sh safety $HOME/Downloads)
# ~/Downloads
local input="${@//$HOME/~}" from to list
# dbg "Got input: $input" 1>&2
for list in $HOME/.safety.list; do
if [ -f $list ]; then
while read from to; do
input="$(printf "$input" | sed -E "s,$from,$to,g")"
done <$list
fi
done
if is_zsh_strict; then
# if running under zsh mode
if command -v hash >/dev/null; then
hash -d | while IFS=$'=' read to from; do
from="$(echo $from | tr -d "\042")"
input="$(printf "$input" | sed -E "s,$from,~$to,g")"
done
fi
elif command -v zsh >/dev/null; then
# in bash/sh mode
[ -f /tmp/hash.list ] || zsh -c "hash -d|sed 's/=/:/'|tr -d \"'\"|IFS=\$':' sort -k2 -r" >/tmp/hash.list
while IFS=$':' read to from; do
from="$(eval printf '%s' $from)"
to="$(eval printf '%s' $to)"
# echo " $from -> $to" 1>&2
# echo "$input" | sed -E 's,'"$from"',~'"$to"',g' 1>&2
input="$(printf "$input" | sed -E 's,'"$from"',~'"$to"',g')"
done </tmp/hash.list
fi
# in="$(echo $in | sed -E -e "s,/Volumes/Vol,~vol,g")"
printf "$input"
}
safetypipe() { while read line; do printf "$(safety $line)"; done; }
datename() {
local i=${1:-7}
if [[ $OSTYPE == darwin* ]]; then
date -v-${i}d +%Y-%m-%d
else
date -d -${i}day +%Y-%m-%d
fi
}
for_each_days() {
# Sample:
#
# delete_log_file() {
# local dtname="$1"
# for PRE in .sizes db-bacup tool-updates; do
# $SUDO find . -type f -iname "${PRE}.$dtname"'*'".log" -print -delete | pad 3 "" " deleted."
# done
# }
#
# delete_elder_logs() {
# for_each_days delete_log_file 7 # delete the older logfiles more than 7 days
# }
local func="$1" && (($#)) && shift
local DAYS1="${1:-30}" && (($#)) && shift
local TILLDAYS=365
dbg "func: $func, days: $DAYS1"
# local TILLDAYS=$((DAYS1 + 365))
for ((i = $DAYS1; i < $TILLDAYS; i++)); do
eval $func "$(datename $i)" "$@"
done
}
commander() {
local commander_self="$1" && (($#)) && shift
local commander_cmd="${1:-usage}" && (($#)) && shift
case $commander_cmd in
help | usage | --help | -h | -H) "${commander_self}_usage" "$@" ;;
funcs | --funcs | --functions | --fn | -fn) script_functions "^$commander_self" ;;
*)
# if [ "$(type -t ${commander_self}_${commander_cmd}_entry)" == "function" ]; then
if fn_exists ${commander_self}_${commander_cmd}_entry; then
dbg "try invoking: ${commander_self}_${commander_cmd}_entry | $@"
eval ${commander_self}_${commander_cmd}_entry "$@"
elif fn_exists ${commander_self}-${commander_cmd}-entry; then
eval ${commander_self}-${commander_cmd}-entry "$@"
elif fn_exists ${commander_self}-${commander_cmd}; then
eval ${commander_self}-${commander_cmd} "$@"
elif fn_exists ${commander_self}-${commander_cmd//_/-}; then
eval ${commander_self}-${commander_cmd//_/-} "$@"
elif fn_exists ${commander_self}_${commander_cmd//-/_}; then
eval ${commander_self}_${commander_cmd//-/_} "$@"
else
dbg "try invoking: ${commander_self}_${commander_cmd} | $@"
eval ${commander_self}_${commander_cmd} "$@"
fi
;;
esac
}
#
#
hex2ip4() { local II="$1" && echo "$(((II >> 24) & 0xff)).$(((II >> 16) & 0xff)).$(((II >> 8) & 0xff)).$((II & 0xff))"; }
ip_hex() {
tox() {
local IP S A II
while IFS='/' read IP S; do
is_bash_strict && {
IFS='.' read -ra A <<<"$IP"
II=$(printf '0x%02X%02X%02X%02X' ${A[0]} ${A[1]} ${A[2]} ${A[3]})
echo $II
} || bash <<-EOF
IFS='.' read -ra A <<<"$IP"
II=\$(printf '0x%02X%02X%02X%02X' \${A[0]} \${A[1]} \${A[2]} \${A[3]})
echo \$II
EOF
done
}
lanip | tox
}
netmask_hex() {
tox() {
local IP S M
while IFS='/' read IP S; do
M=$((0xffffffff ^ ((1 << (32 - S)) - 1)))
printf '0x%08x' $M
done
}
lanip | tox
}
subnet_hex() {
tox1() {
# local IP S M A I II
while IFS='/' read IP S; do
is_bash_strict && {
# tip "ip: $IP, S: $S"
M=$((0xffffffff ^ ((1 << (32 - S)) - 1)))
IFS=. read -ra A <<<"$IP"
# tip "A: ${A[@]}, M: $(printf '0x%08x' $M)"
I=$(printf '0x%02X%02X%02X%02X' ${A[0]} ${A[1]} ${A[2]} ${A[3]})
II=$((M & I))
printf '0x%08x' $II
} || bash <<-EOF
M=\$((0xffffffff ^ ((1 << (32 - $S)) - 1)))
IFS=. read -ra A <<<"$IP"
# tip "A: ${A[@]}, M: $(printf '0x%08x' $M)"
I=\$(printf '0x%02X%02X%02X%02X' \${A[0]} \${A[1]} \${A[2]} \${A[3]})
II=\$((M & I))
printf '0x%08x' \$II
EOF
done
}
# tip "lanip: '$(lanip)'"
lanip | tox1
}
if is_darwin; then
readlinkx() {
local p="$@"
[ -L "$@" ] && p="$(readlink "$@")"
echo "$p"
}
realpathx() {
if [[ $1 == /* ]]; then
# dbg " .. case 1: '$1'"
echo "$@"
else
local DIR="${1%/*}" d p
if [ -d "$DIR" ]; then
# dbg " .. case 2: '$1' / DIR = '$DIR' pwd=$(pwd -P)"
DIR="$(cd $DIR && pwd -P)"
d="$DIR/$(basename "$1")"
p="$(readlinkx "$d")"
else
# dbg " .. case 3: '$1'"
p="$(readlinkx "$@")"
fi
# dbg " p: '$p', d: '$d'"
[[ $p == /* ]] && echo "$p" || {
[[ "$p" == "" ]] && echo || {
local DIR="${p%/*}" && {
[ -d "$DIR" ] && { DIR=$(cd $DIR && pwd -P) && echo "$DIR/$(basename $p)"; } || echo "$p"
}
}
}
fi
}
hex2mask() {
local hexmask=$(echo $1 | sed -e 's/^0x//')
local i
# printf "(%s)" $hexmask
for ((i = 0; i < ${#hexmask}; i += 2)); do
if (($i > 1)); then
# use a . to separate octets
# but don't print a leading .
printf "%s" "."
fi
printf "%d" "0x${hexmask:$i:2}"
done
printf "\n"
}
default_dev() { route get default | awk '/interface:/{print $2}'; }
gw() { route get default | awk '/gateway:/{print $2}'; }
lanip() { ifconfig | grep 'inet ' | grep -vE '127.0.0.1|::1|%lo|fe80::' | awk '{print $2}'; }
lanip6() { ifconfig | grep 'inet6 ' | grep -vE '127.0.0.1|::1|%lo|fe80::' | awk '{print $2}'; }
lanipall() { ifconfig | grep -P 'inet6? ' | grep -vE '127.0.0.1|::1|%lo|fe80::' | awk '{print $2}'; }
netmask_hex() { ifconfig $(default_dev) | awk '/netmask /{print $4}'; }
else
ipcmd="$(which ip 1>/dev/null 2>&1 && echo 'sudo ip' || echo ifconfig)"
realpathx() { readlink -f "$@"; }
default_dev() { eval $ipcmd route show default | grep -oE 'dev \w+' | awk '{print $2}'; }
if is_suse_series; then
gw() { which netstat 1>/dev/null 2>&1 && netstat -r -n | grep -P '^0.0.0.0' | awk '{print $2}' || {
if eval "$ipcmd route show" | grep -qP '^default'; then
eval "$ipcmd route show default" | awk '{print $3}'
else
local xx=$(eval "$ipcmd route show" | awk '{print $1}')
if [[ "$xx" = */* ]]; then
cut -d'/' -f1 <<<"$xx" | sed 's/.0$/.1/'
else
echo $xx
fi
fi
}; }
else
gw() { eval "$ipcmd route show default" | awk '{print $3}'; }
fi
lanip() { eval $ipcmd a | grep -E 'inet ' | grep -vE '127.0.0.1|::1|%lo|fe80::' | awk '{print $2}'; }
lanip6() { eval $ipcmd a | grep 'inet6 ' | grep -vE '127.0.0.1|::1|%lo|fe80::' | awk '{print $2}'; }
lanipall() { eval $ipcmd a | grep -E 'inet6? ' | grep -vE '127.0.0.1|::1|%lo|fe80::' | awk '{print $2}'; }
fi
subnet4() { hex2ip4 $(subnet_hex); }
netmask() { hex2ip4 $(netmask_hex); }
# alias wanip='dig +short myip.opendns.com @resolver1.opendns.com'
# alias ip-wan=wanip
wanip() { host myip.opendns.com 208.67.220.222 | tail -1 | awk '{print $4}'; }
wanip6() { host -t AAAA myip.opendns.com resolver1.ipv6-sandbox.opendns.com | grep -oE "^myip\.opendns\.com.*" | awk '{print $5}'; }
# use a tool script 'externalip' is better choice.
# try more sources for yourself:
# http://ipecho.net/plain
# http://ifcfg.me/
# ...
wanip_http() { curl -s http://whatismyip.akamai.com/; }
# the best and exact way is asking a dns server by dig/host:
wanip_exact() { dig @resolver4.opendns.com myip.opendns.com +short; }
wanip6_exact() { dig @resolver1.ipv6-sandbox.opendns.com AAAA myip.opendns.com +short -6; }
#
#
main_do_sth() {
[ ${VERBOSE:-0} -eq 1 ] && set -x
set -e
# set -o errexit
# set -o nounset
# set -o pipefail
MAIN_DEV=${MAIN_DEV:-$(default_dev)}
MAIN_ENTRY=${MAIN_ENTRY:-_my_main_do_sth}
local res_
in_debug && debug_info && dbg "$(safety "$MAIN_ENTRY - $@\n [CD: $CD, SCRIPT: $SCRIPT]")"
if in_sourcing; then
$MAIN_ENTRY "$@"
res_=$?
else
trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
trap '[ $? -ne 0 ] && echo FAILED COMMAND: "$previous_command" with exit code $?' EXIT
$MAIN_ENTRY "$@"
res_=$?
trap - EXIT
fi
((${HAS_END:-0})) && { debug_begin && echo -n 'Success!' && debug_end; } || return $res_ # { [ $# -eq 0 ] && :; }
}
BASH_SH_VERSION=v20241112
DEBUG=${DEBUG:-0}
# trans_readlink() { DIR="${1%/*}" && (cd $DIR && pwd -P); }
# is_darwin && realpathx() { [[ $1 == /* ]] && echo "$1" || { DIR="${1%/*}" && DIR=$(cd $DIR && pwd -P) && echo "$DIR/$(basename $1)"; }; } || realpathx() { readlink -f $*; }
in_sourcing && { CD="${CD}" && debug ">> IN SOURCING, \$0=$0, \$_=$_"; } || { SCRIPT=$(realpathx "$0") && CD=$(dirname "$SCRIPT") && debug ">> '$SCRIPT' in '$CD', \$0='$0','$1'."; }
if_vagrant && [ "$SCRIPT" == "/tmp/vagrant-shell" ] && { [ -d $CD/ops.d ] || CD=/vagrant/bin; }
[ -L "$SCRIPT" ] && debug linked script found && SCRIPT=$(realpathx "$SCRIPT") && CD=$(dirname "$SCRIPT")
in_sourcing || main_do_sth "$@"
#### HZ Tail END #### v20241112 ####