Merge branch 'master' of git.axenov.dev:anthony/shell

This commit is contained in:
2025-03-18 13:45:33 +08:00
16 changed files with 422 additions and 80 deletions

View File

@@ -85,6 +85,9 @@ where:
* https://lug.fh-swf.de/vim/vim-bash/StyleGuideShell.en.pdf
* https://www.thegeekstuff.com/2010/06/bash-array-tutorial/
* 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

View File

@@ -5,6 +5,34 @@ source $( dirname $(readlink -e -- "${BASH_SOURCE}"))/io.sh || exit 255
# 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
abspath() {
echo $(realpath -q "${1/#\~/$HOME}")
@@ -93,3 +121,7 @@ is_int() {
is_number() {
[[ "$1" =~ ^[0-9]+([.,][0-9]+)?$ ]]
}
trim() {
echo "$1" | xargs
}

View File

@@ -46,3 +46,34 @@ docker.exec() {
debug "Команда: $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
View 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"
}

View File

@@ -76,9 +76,14 @@ ask() {
}
print() {
# if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID > /dev/null; fi
echo -e "$*${FRESET}"
}
link() {
echo -e "\e]8;;$2\a$1\e]8;;\a"
}
debug() {
if [ "$2" ]; then
print "${FDIM}${FBOLD}${FRESET}${FDIM}$(now)${IDEBUG} ${FUNCNAME[1]:-?}():${BASH_LINENO:-?}\t$1 " >&2
@@ -141,20 +146,53 @@ die() {
exit ${2:-255}
}
var='test var_dump'
var_dump var
debug 'test debug'
verbose 'test verbose'
info 'test info'
note 'test note'
success 'test success'
warn 'test warn'
error 'test error'
fatal 'test fatal'
die 'test die'
# var='test var_dump'
# var_dump var
# debug 'test debug'
# verbose 'test verbose'
# info 'test info'
# note 'test note'
# success 'test success'
# warn 'test warn'
# error 'test error'
# fatal 'test fatal'
# die 'test die'
# 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://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
# https://linuxcommand.org/lc3_adv_tput.php

View File

@@ -6,10 +6,10 @@
# https://askubuntu.com/a/30414
is_full_screen() {
local WINDOW=$(echo $(xwininfo -id $(xdotool getactivewindow) -stats | \
egrep '(Width|Height):' | \
awk '{print $NF}') | \
sed -e 's/ /x/')
local WINDOW=$(xwininfo -id "$(xdotool getactivewindow)" -stats \
| grep -E '(Width|Height):' \
| awk '{print $NF}' \
| sed -e 's/ /x/')
local SCREEN=$(xdpyinfo | grep -m1 dimensions | awk '{print $2}')
if [ "$WINDOW" = "$SCREEN" ]; then
return 0
@@ -17,19 +17,6 @@ is_full_screen() {
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() {
youtube-dl \
--extract-audio \
@@ -38,29 +25,5 @@ ytm() {
--format bestaudio \
--write-info-json \
--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
View 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
}

63
helpers/notif.sh Normal file
View File

@@ -0,0 +1,63 @@
#!/usr/bin/env bash
########################################################
# Notifications
########################################################
TITLE="$0"
NTFY_CHANNEL="example"
# отправляет простую нотификацию
ntfy_info() {
require ntfy
ntfy send \
--title "$TITLE" \
--message "$1" \
--priority 1 \
"$NTFY_CHANNEL"
}
# отправляет нотификацию с предупреждением
ntfy_warn() {
require ntfy
ntfy send \
--title "$TITLE" \
--tags "warning" \
--message "$1" \
--priority 5 \
"$NTFY_CHANNEL"
}
notify () {
if ! installed "notify-send"; then
warning "Notifications toggled on, but 'notify-send' is not installed!"
return 1
fi
[ -n "$1" ] && local title="$1"
local text="$2"
local level="$3"
local icon="$4"
case "$level" in
critical) local timeout=0 ;;
low) local timeout=5000 ;;
*) local timeout=10000 ;;
esac
debug "$title / $text / $level / $icon / $timeout"
notify-send "$title" "$text" -a "$0" -u "$level" -i "$icon" -t $timeout
}
# TODO: docblock
notify_error() {
notify "Error" "$1" "critical" "dialog-error"
}
# TODO: docblock
notify_warning() {
notify "Warning" "$1" "normal" "dialog-warning"
}
# TODO: docblock
notify_info() {
notify "" "$1" "low" "dialog-information"
}

21
helpers/traps.sh Normal file
View 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
}

6
install/clamav Executable file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
##makedesc: ClamAV (WIP)
sudo apt install clamav -y && \
echo "DatabaseMirror packages.microsoft.com/clamav" | sudo tee -a /etc/clamav/freshclam.conf && \
sudo freshclam

View File

@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# Original filename: docker-volume-snapshot
# Author: Juned Khatri
# License: MIT
# Repo: https://github.com/junedkhatri31/docker-volume-snapshot
set -e -o pipefail
programname=`basename "$0"`
display_usage() {
echo "usage: $programname (create|restore) source destination"
echo " create create snapshot file from docker volume"
echo " restore restore snapshot file to docker volume"
echo " source source path"
echo " destination destination path"
echo
echo "Tip: Supports tar's compression algorithms automatically"
echo " based on the file extention, for example .tar.gz"
echo
echo "Examples:"
echo "docker-volume-snapshot create xyz_volume xyz_volume.tar"
echo "docker-volume-snapshot create xyz_volume xyz_volume.tar.gz"
echo "docker-volume-snapshot restore xyz_volume.tar xyz_volume"
echo "docker-volume-snapshot restore xyz_volume.tar.gz xyz_volume"
}
case "$1" in
"create")
if [[ -z "$2" || -z "$3" ]]; then display_usage; exit 1; fi
directory=`dirname "$3"`
if [ "$directory" == "." ]; then directory=$(pwd); fi
filename=`basename "$3"`
docker run --rm -v "$2:/source" -v "$directory:/dest" busybox tar cvaf "/dest/$filename" -C /source .
;;
"restore")
if [[ -z "$2" || -z "$3" ]]; then display_usage; exit 1; fi
directory=`dirname "$2"`
if [ "$directory" == "." ]; then directory=$(pwd); fi
filename=`basename "$2"`
docker run --rm -v "$3:/dest" -v "$directory:/source" busybox tar xvf "/source/$filename" -C /dest
;;
*)
display_usage
exit 1 # Command to come out of the program with status 1
;;
esac

View File

@@ -3,46 +3,60 @@
# https://gist.github.com/anthonyaxenov/02c00c965be4eb5bb163a153abdf4c2b
# https://itsfoss.com/free-up-space-ubuntu-linux/
df -h
echo ""
echo
echo
df -hx tmpfs
echo
echo
echo "[1/5] Removing apt caches and unused packages"
echo ""
echo
sudo apt autoremove --purge
sudo apt autoclean
sudo apt clean
echo ""
echo "[2/5] Removing old journalctl logs"
echo ""
echo
echo "[2/5] Removing old system logs"
echo
sudo journalctl --vacuum-time=1d
sudo rm -rf /var/log/journal/user-*@*
sudo rm -rf /var/log/journal/system*@*
sudo rm /var/log/{syslog,dmesg,btmp}.*
sudo rm /var/log/{auth,dpkg,kern,alternatives,dmesg}.log.*
echo ""
echo
echo "[3/5] Cleaning user trash and thumbnails"
echo ""
echo
rm -rf ~/.local/share/Trash/files/*
rm -rf ~/.cache/thumbnails/*
echo ""
echo
echo "[4/5] Cleaning out dangling docker objects"
echo ""
echo
docker system prune -f
# docker system prune -af
echo ""
echo
echo "[5/5] Removing disabled unused snaps"
echo ""
echo
sudo snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
sudo snap remove "$snapname" --revision="$revision"
done
sudo snap list --all \
| awk '/disabled/{print $1, $3}' \
| while read snapname revision; do
sudo snap remove "$snapname" --revision="$revision"
done
echo ""
echo ""
df -h
echo
echo
df -hx tmpfs
echo
echo

View File

@@ -29,4 +29,5 @@ apt install -y \
mariadb-server \
mariadb-client \
nginx \
certbot
certbot \
python3-certbot-nginx

View File

@@ -41,13 +41,6 @@ sudo apt install -y \
notify-osd \
fonts-open-sans \
libnotify-bin \
gnome-software \
gnome-software-plugin-flatpak \
gnome-software-plugin-snap \
terminator \
geoclue-2.0 \
redshift \
redshift-gtk \
samba \
dkms

51
tools/ubuntu-server.sh Normal file
View File

@@ -0,0 +1,51 @@
#!/usr/bin/env bash
sudo apt install -y ca-certificates curl && \
sudo install -m 0755 -d /etc/apt/keyrings && \
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc && \
sudo chmod a+r /etc/apt/keyrings/docker.asc && \
source /etc/os-release && \
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null && \
sudo apt update && \
sudo apt upgrade -y --autoremove && \
sudo apt install -y \
apt-transport-https \
build-essential \
ca-certificates \
git \
cmake \
curl \
dialog \
gettext \
gnupg \
htop \
libcurl4-gnutls-dev \
libexpat1-dev \
libghc-zlib-dev \
libssl-dev \
lsb-release \
make \
mc \
meld \
nano \
neofetch \
net-tools \
nmap \
p7zip-full \
unzip \
ffmpeg \
inotify-tools \
notify-osd \
fonts-open-sans \
libnotify-bin \
tree \
nginx \
certbot \
python3-certbot-nginx \
docker-ce \
docker-ce-cli \
containerd.io \
docker-buildx-plugin \
docker-compose-plugin