Compare commits
6 Commits
9da84a69f2
...
16075cdd61
| Author | SHA1 | Date | |
|---|---|---|---|
|
16075cdd61
|
|||
|
29e5c960de
|
|||
|
526dade1f8
|
|||
|
66126167c8
|
|||
|
3696cc06af
|
|||
|
8da2059d1e
|
@@ -85,6 +85,9 @@ where:
|
|||||||
* https://lug.fh-swf.de/vim/vim-bash/StyleGuideShell.en.pdf
|
* https://lug.fh-swf.de/vim/vim-bash/StyleGuideShell.en.pdf
|
||||||
* https://www.thegeekstuff.com/2010/06/bash-array-tutorial/
|
* https://www.thegeekstuff.com/2010/06/bash-array-tutorial/
|
||||||
* https://www.distributednetworks.com/linux-network-admin/module4/ephemeral-reserved-portNumbers.php
|
* https://www.distributednetworks.com/linux-network-admin/module4/ephemeral-reserved-portNumbers.php
|
||||||
|
* https://github.com/community-scripts/ProxmoxVE/tree/main/install
|
||||||
|
* https://github.com/community-scripts/ProxmoxVE/tree/main/misc
|
||||||
|
* https://faculty.cs.niu.edu/~hutchins/csci480/signals.htm
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,34 @@ source $( dirname $(readlink -e -- "${BASH_SOURCE}"))/io.sh || exit 255
|
|||||||
# Little handy helpers for scripting
|
# Little handy helpers for scripting
|
||||||
########################################################
|
########################################################
|
||||||
|
|
||||||
|
is_bash() {
|
||||||
|
[[ "$(basename "$SHELL")" != "bash" ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
is_sourced() {
|
||||||
|
[[ "${BASH_SOURCE[0]}" != "$0" ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
is_root() {
|
||||||
|
[[ "$(id -u)" -ne 0 || $(ps -o comm= -p $PPID) == "sudo" ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
get_os() {
|
||||||
|
case "$(uname -s)" in
|
||||||
|
Linux*) echo Linux ;;
|
||||||
|
Darwin*) echo Macos ;;
|
||||||
|
CYGWIN*) echo Cygwin ;;
|
||||||
|
MINGW*) echo MinGw ;;
|
||||||
|
MSYS_NT*) echo Git ;;
|
||||||
|
*) return 1 ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
get_os_id() {
|
||||||
|
[ -f /etc/os-release ] && source /etc/os-release
|
||||||
|
echo "$ID"
|
||||||
|
}
|
||||||
|
|
||||||
# convert relative path $1 to full one
|
# convert relative path $1 to full one
|
||||||
abspath() {
|
abspath() {
|
||||||
echo $(realpath -q "${1/#\~/$HOME}")
|
echo $(realpath -q "${1/#\~/$HOME}")
|
||||||
@@ -93,3 +121,7 @@ is_int() {
|
|||||||
is_number() {
|
is_number() {
|
||||||
[[ "$1" =~ ^[0-9]+([.,][0-9]+)?$ ]]
|
[[ "$1" =~ ^[0-9]+([.,][0-9]+)?$ ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trim() {
|
||||||
|
echo "$1" | xargs
|
||||||
|
}
|
||||||
|
|||||||
@@ -46,3 +46,34 @@ docker.exec() {
|
|||||||
debug "Команда: $cmd"
|
debug "Команда: $cmd"
|
||||||
$cmd
|
$cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Выводит информацию о контейнере
|
||||||
|
docker.inspect() {
|
||||||
|
cmd="docker inspect $*"
|
||||||
|
debug "Команда: $cmd"
|
||||||
|
$cmd 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
docker.ip() { # not finished
|
||||||
|
if [ "$1" ]; then
|
||||||
|
if [ "$1" = "-a" ]; then
|
||||||
|
docker ps -aq \
|
||||||
|
| xargs -n 1 docker inspect --format '{{.Name}}{{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' \
|
||||||
|
| sed -e 's#^/##' \
|
||||||
|
| column -t
|
||||||
|
elif [ "$1" = "-c" ]; then
|
||||||
|
docker-compose ps -q \
|
||||||
|
| xargs -n 1 docker inspect --format '{{.Name}}{{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' \
|
||||||
|
| sed -e 's#^/##' \
|
||||||
|
| column -t
|
||||||
|
else
|
||||||
|
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$1"
|
||||||
|
docker port "$1"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
docker ps -q \
|
||||||
|
| xargs -n 1 docker inspect --format '{{.Name}}{{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' \
|
||||||
|
| sed -e 's#^/##' \
|
||||||
|
| column -t
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|||||||
28
helpers/help.sh
Normal file
28
helpers/help.sh
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
#TODO source basic.sh
|
||||||
|
#TODO source args-parser/args.sh
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
# Help functions
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
process_help_arg() {
|
||||||
|
command="${FUNCNAME[1]}"
|
||||||
|
need_help=$(arg help 1)
|
||||||
|
[[ "$need_help" -eq 0 ]] && need_help=$(argl help 1)
|
||||||
|
[[ "$need_help" -eq 1 ]] && help "$command"
|
||||||
|
}
|
||||||
|
|
||||||
|
help() {
|
||||||
|
is_function "help.$1" && help."$1" && exit
|
||||||
|
echo "Main help message"
|
||||||
|
}
|
||||||
|
|
||||||
|
help.example() {
|
||||||
|
echo "Example help message"
|
||||||
|
}
|
||||||
|
|
||||||
|
example() {
|
||||||
|
process_help_arg
|
||||||
|
echo "Example command"
|
||||||
|
}
|
||||||
@@ -76,9 +76,14 @@ ask() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
print() {
|
print() {
|
||||||
|
# if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID > /dev/null; fi
|
||||||
echo -e "$*${FRESET}"
|
echo -e "$*${FRESET}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
link() {
|
||||||
|
echo -e "\e]8;;$2\a$1\e]8;;\a"
|
||||||
|
}
|
||||||
|
|
||||||
debug() {
|
debug() {
|
||||||
if [ "$2" ]; then
|
if [ "$2" ]; then
|
||||||
print "${FDIM}${FBOLD}${FRESET}${FDIM}$(now)${IDEBUG} ${FUNCNAME[1]:-?}():${BASH_LINENO:-?}\t$1 " >&2
|
print "${FDIM}${FBOLD}${FRESET}${FDIM}$(now)${IDEBUG} ${FUNCNAME[1]:-?}():${BASH_LINENO:-?}\t$1 " >&2
|
||||||
@@ -141,20 +146,53 @@ die() {
|
|||||||
exit ${2:-255}
|
exit ${2:-255}
|
||||||
}
|
}
|
||||||
|
|
||||||
var='test var_dump'
|
# var='test var_dump'
|
||||||
var_dump var
|
# var_dump var
|
||||||
debug 'test debug'
|
# debug 'test debug'
|
||||||
verbose 'test verbose'
|
# verbose 'test verbose'
|
||||||
info 'test info'
|
# info 'test info'
|
||||||
note 'test note'
|
# note 'test note'
|
||||||
success 'test success'
|
# success 'test success'
|
||||||
warn 'test warn'
|
# warn 'test warn'
|
||||||
error 'test error'
|
# error 'test error'
|
||||||
fatal 'test fatal'
|
# fatal 'test fatal'
|
||||||
die 'test die'
|
# die 'test die'
|
||||||
|
|
||||||
# experiments ==============================================================================
|
# experiments ==============================================================================
|
||||||
|
|
||||||
|
# spinner() {
|
||||||
|
# local frames=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏')
|
||||||
|
# local spin_i=0
|
||||||
|
# local interval=0.1
|
||||||
|
# printf "\e[?25l"
|
||||||
|
|
||||||
|
# local color="${FGREEN}"
|
||||||
|
|
||||||
|
# while true; do
|
||||||
|
# printf "\r ${color}%s${CL}" "${frames[spin_i]}"
|
||||||
|
# spin_i=$(( (spin_i + 1) % ${#frames[@]} ))
|
||||||
|
# sleep "$interval"
|
||||||
|
# done
|
||||||
|
# }
|
||||||
|
|
||||||
|
# echo "lorem ipsum dolor sit amet"
|
||||||
|
# spinner &
|
||||||
|
# SPINNER_PID=$!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ===========
|
||||||
|
|
||||||
|
|
||||||
# https://unix.stackexchange.com/a/269085
|
# https://unix.stackexchange.com/a/269085
|
||||||
# https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
# https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
||||||
# https://linuxcommand.org/lc3_adv_tput.php
|
# https://linuxcommand.org/lc3_adv_tput.php
|
||||||
|
|||||||
@@ -6,10 +6,10 @@
|
|||||||
|
|
||||||
# https://askubuntu.com/a/30414
|
# https://askubuntu.com/a/30414
|
||||||
is_full_screen() {
|
is_full_screen() {
|
||||||
local WINDOW=$(echo $(xwininfo -id $(xdotool getactivewindow) -stats | \
|
local WINDOW=$(xwininfo -id "$(xdotool getactivewindow)" -stats \
|
||||||
egrep '(Width|Height):' | \
|
| grep -E '(Width|Height):' \
|
||||||
awk '{print $NF}') | \
|
| awk '{print $NF}' \
|
||||||
sed -e 's/ /x/')
|
| sed -e 's/ /x/')
|
||||||
local SCREEN=$(xdpyinfo | grep -m1 dimensions | awk '{print $2}')
|
local SCREEN=$(xdpyinfo | grep -m1 dimensions | awk '{print $2}')
|
||||||
if [ "$WINDOW" = "$SCREEN" ]; then
|
if [ "$WINDOW" = "$SCREEN" ]; then
|
||||||
return 0
|
return 0
|
||||||
@@ -17,19 +17,6 @@ is_full_screen() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
curltime() {
|
|
||||||
curl -w @- -o /dev/null -s "$@" <<'EOF'
|
|
||||||
time_namelookup: %{time_namelookup} sec\n
|
|
||||||
time_connect: %{time_connect} sec\n
|
|
||||||
time_appconnect: %{time_appconnect} sec\n
|
|
||||||
time_pretransfer: %{time_pretransfer} sec\n
|
|
||||||
time_redirect: %{time_redirect} sec\n
|
|
||||||
time_starttransfer: %{time_starttransfer} sec\n
|
|
||||||
---------------\n
|
|
||||||
time_total: %{time_total} sec\n
|
|
||||||
EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
ytm() {
|
ytm() {
|
||||||
youtube-dl \
|
youtube-dl \
|
||||||
--extract-audio \
|
--extract-audio \
|
||||||
@@ -38,29 +25,5 @@ ytm() {
|
|||||||
--format bestaudio \
|
--format bestaudio \
|
||||||
--write-info-json \
|
--write-info-json \
|
||||||
--output "$HOME/Downloads/ytm/%(playlist_title)s/%(channel)s - %(title)s.%(ext)s" \
|
--output "$HOME/Downloads/ytm/%(playlist_title)s/%(channel)s - %(title)s.%(ext)s" \
|
||||||
$*
|
"$@"
|
||||||
}
|
|
||||||
|
|
||||||
docker.ip() { # not finished
|
|
||||||
if [ "$1" ]; then
|
|
||||||
if [ "$1" = "-a" ]; then
|
|
||||||
docker ps -aq \
|
|
||||||
| xargs -n 1 docker inspect --format '{{.Name}}{{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' \
|
|
||||||
| sed -e 's#^/##' \
|
|
||||||
| column -t
|
|
||||||
elif [ "$1" = "-c" ]; then
|
|
||||||
docker-compose ps -q \
|
|
||||||
| xargs -n 1 docker inspect --format '{{.Name}}{{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' \
|
|
||||||
| sed -e 's#^/##' \
|
|
||||||
| column -t
|
|
||||||
else
|
|
||||||
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$1"
|
|
||||||
docker port "$1"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
docker ps -q \
|
|
||||||
| xargs -n 1 docker inspect --format '{{.Name}}{{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' \
|
|
||||||
| sed -e 's#^/##' \
|
|
||||||
| column -t
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|||||||
51
helpers/net.sh
Normal file
51
helpers/net.sh
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
# Networking functions
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
get_current_ip() {
|
||||||
|
local CURRENT_IP
|
||||||
|
[ -f /etc/os-release ] && source /etc/os-release
|
||||||
|
case "$ID" in
|
||||||
|
debian|ubuntu) CURRENT_IP=$(hostname -I | awk '{print $1}') ;;
|
||||||
|
alpine) CURRENT_IP=$(ip -4 addr show eth0 | awk '/inet / {print $2}' | cut -d/ -f1 | head -n 1) ;;
|
||||||
|
*) CURRENT_IP="Unknown" ;;
|
||||||
|
esac
|
||||||
|
echo "$CURRENT_IP"
|
||||||
|
}
|
||||||
|
|
||||||
|
get_external_ip() {
|
||||||
|
local ip="$(curl -s https://api.myip.com | jq .ip)"
|
||||||
|
echo "$ip" | tr -d '"'
|
||||||
|
}
|
||||||
|
|
||||||
|
is_valid_ipv4() {
|
||||||
|
local ip="$1"
|
||||||
|
local regex="^([0-9]{1,3}\.){3}[0-9]{1,3}$"
|
||||||
|
|
||||||
|
if [[ $ip =~ $regex ]]; then
|
||||||
|
IFS='.' read -r -a parts <<< "$ip"
|
||||||
|
for part in "${parts[@]}"; do
|
||||||
|
if ! [[ $part =~ ^[0-9]+$ ]] || ((part < 0 || part > 255)); then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
curltime() {
|
||||||
|
curl -w @- -o /dev/null -s "$@" <<'EOF'
|
||||||
|
time_namelookup: %{time_namelookup} sec\n
|
||||||
|
time_connect: %{time_connect} sec\n
|
||||||
|
time_appconnect: %{time_appconnect} sec\n
|
||||||
|
time_pretransfer: %{time_pretransfer} sec\n
|
||||||
|
time_redirect: %{time_redirect} sec\n
|
||||||
|
time_starttransfer: %{time_starttransfer} sec\n
|
||||||
|
---------------\n
|
||||||
|
time_total: %{time_total} sec\n
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
21
helpers/traps.sh
Normal file
21
helpers/traps.sh
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
#TODO source basic.sh
|
||||||
|
#TODO source args-parser/args.sh
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
# Trap usage examples
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
# for sig in SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGTERM SIGTSTP; do
|
||||||
|
# # shellcheck disable=SC2064
|
||||||
|
# trap "set +x && echo && echo && echo '*** Прервано сигналом $sig, остановка ***' && exit" $sig
|
||||||
|
# done
|
||||||
|
|
||||||
|
for sig in SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGTERM SIGTSTP; do
|
||||||
|
trap "myfunc" $sig
|
||||||
|
done
|
||||||
|
|
||||||
|
myfunc() {
|
||||||
|
echo "trapped!"
|
||||||
|
exit
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user