refactor bash functions

This commit is contained in:
thek4n 2024-09-06 14:55:01 +03:00
parent dc9260af5a
commit 6933160f7d

View File

@ -2,9 +2,10 @@
_sod() { _sod() {
if [ -d "$1" ] && [ -n "$(ls "$1")" ]; then if [ -d "$1" ] && [ -n "$(ls "$1")" ]; then
for FILENAME in $(ls "$1" | sort -n) local filename
for filename in $(ls "$1" | sort -n)
do do
source "$1/$FILENAME" source "${1}/${filename}"
done done
fi fi
} }
@ -23,14 +24,14 @@ destroy() {
} }
workon() { workon() {
source "/opt/pythonenv/${1:-$(basename $PWD)}/bin/activate" source "/opt/pythonenv/${1:-$(basename "$PWD")}/bin/activate"
} }
py() { py() {
if [[ -z "$@" && -x "$(command -v ipython 2>/dev/null)" ]]; then if [[ -z "$@" && -x "$(command -v ipython 2>/dev/null)" ]]; then
ipython -i -c "q = exit" ipython -i -c "q = exit"
else else
python3 $@ python3 "$@"
fi fi
} }
@ -39,14 +40,14 @@ ve() {
} }
va() { va() {
local activate_venv
if [ -f "./venv/bin/activate" ]; then if [ -f "./venv/bin/activate" ]; then
source "./venv/bin/activate" source "./venv/bin/activate"
return 0 return 0
fi fi
local activate_venv
activate_venv="$(find -P . -maxdepth 3 -type f -wholename '*/bin/activate' | sort | head -n 1)" activate_venv="$(find -P . -maxdepth 3 -type f -wholename '*/bin/activate' | sort | head -n 1)"
readonly activate_venv
if [ -n "$activate_venv" ]; then if [ -n "$activate_venv" ]; then
source "$activate_venv" source "$activate_venv"
@ -69,16 +70,13 @@ extract() (
return 1 return 1
fi fi
local NAME local -r name="${1%%.*}" # removes extension from filename
NAME=${1%%.*} # removes extension from filename if [ -e "$name" ]; then
echo "extract: error: '$name' exists" >&2
if [ -e "$NAME" ]; then
echo "extract: error: '$NAME' exists" >&2
return 1 return 1
fi fi
mkdir "$NAME" && cd "$NAME" || return 1 mkdir "$name" && cd "$name" || return 1
case $1 in case $1 in
*.tar.bz2) tar xjf ../"$1" ;; *.tar.bz2) tar xjf ../"$1" ;;
@ -100,7 +98,6 @@ extract() (
esac esac
) )
mcd() { mcd() {
if [ -z "$1" ]; then if [ -z "$1" ]; then
cd "$(mktemp -td "${USER:-user}.XXXX")" cd "$(mktemp -td "${USER:-user}.XXXX")"
@ -109,54 +106,6 @@ mcd() {
fi fi
} }
# toggle wireguard vpn with interface /etc/wireguard/wg0.conf
# to allow sudo user toggle vpn without password:
## sudo groupadd wireguard
## sudo usermod -aG wireguard $USER
vpn() {
local STATUS SERVICE CONFIG CONFIGFILE
CONFIG="${1:-wg0}"
CONFIGFILE="/etc/wireguard/$CONFIG.conf"
if [ ! -e "$CONFIGFILE" ]; then
echo "Config '$CONFIGFILE' not exists" >&2
return 1
fi
SERVICE="wg-quick@$CONFIG.service"
if systemctl --quiet is-active "$SERVICE"; then
sudo systemctl stop "$SERVICE"
echo "DOWN"
else
sudo systemctl start "$SERVICE" && ip -br a | grep "$CONFIG" | awk '{print $1":\t"$3}'
fi
}
toggle-ssh-proxy() {
if [ -z "$1" ]; then
echo "Proxy ssh server hostname not specified" 1>&2
return 1
fi
SSH_PROXY_CONTROL_FILE="$XDG_RUNTIME_DIR/ssh_${1}_proxy.control"
if [ -e "$SSH_PROXY_CONTROL_FILE" ]; then
unset {http,https,ftp,rsync,no}_proxy
ssh -S "$SSH_PROXY_CONTROL_FILE" -O exit "$1"
unset SSH_PROXY_PID
echo "Disabled"
else
ssh -nCTN -S "$SSH_PROXY_CONTROL_FILE" -D 1080 "$1"
export {http,https,ftp,rsync}_proxy="socks5://127.0.0.1:1080" && \
export no_proxy="localhost,127.0.0.1/8,localaddress,.localdomain.com,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12"
echo "Enabled"
fi
}
open() { open() {
test -e $1 || return 1 test -e $1 || return 1
nohup xdg-open $1 1>/dev/null 2>&1 & nohup xdg-open $1 1>/dev/null 2>&1 &
@ -164,12 +113,12 @@ open() {
split-file() { split-file() {
test -e "$1" || return 1 test -e "$1" || return 1
local _size="${2:-1G}" local -r size="${2:-1G}"
if [ -d "$1" ]; then if [ -d "$1" ]; then
(mkdir "$1.tar.gz.splitted" && cd "$1.tar.gz.splitted" && tar -cz "../$1" | split -d -b "$_size") (mkdir "$1.tar.gz.splitted" && cd "$1.tar.gz.splitted" && tar -cz "../$1" | split -d -b "$size")
else else
(mkdir "$1.splitted" && cd "$1.splitted" && split -d -b "$_size" "../$1") (mkdir "$1.splitted" && cd "$1.splitted" && split -d -b "$size" "../$1")
fi fi
} }
@ -184,6 +133,8 @@ json() {
_get_full_file_extension() { _get_full_file_extension() {
local filename local filename
filename=$(basename "$1") filename=$(basename "$1")
readonly filename
if [ "${filename:0:1}" = "." ]; then if [ "${filename:0:1}" = "." ]; then
filename="${filename:1}" filename="${filename:1}"
fi fi
@ -191,28 +142,31 @@ _get_full_file_extension() {
} }
rmt() { rmt() {
local TRASH local -r TRASH="${HOME}/.trash"
TRASH=~/.trash
local filename local filename
for filename in $* for filename in "$@"
do do
local filename_out_path="${TRASH}$(realpath "$filename")" local filename_out_path
filename_out_path="${TRASH}$(realpath "$filename")"
readonly filename_out_path
mkdir -p "$(dirname "$filename_out_path")" mkdir -p "$(dirname "$filename_out_path")"
mv "$filename" "${filename_out_path}_$(date +%s)" mv "$filename" "${filename_out_path}_$(date +%s)"
done done
} }
showtips() { showtips() {
TIPS_DIR="$HOME/.tips" local -r tips_dir="${HOME}/.tips"
if [ ! -d "$TIPS_DIR" ]; then if [ ! -d "$tips_dir" ]; then
mkdir "$TIPS_DIR" 2>/dev/null mkdir "$tips_dir" 2>/dev/null
git init "$TIPS_DIR" git init "$tips_dir"
fi fi
local content local content
for filename in "$TIPS_DIR"/* local filename
for filename in "$tips_dir"/*
do do
content="$(cat "$filename")" content="$(cat "$filename")"
echo "$content" echo "$content"
@ -226,5 +180,5 @@ most-often-commands() {
# Enable X11Forwarding and disable mux session # Enable X11Forwarding and disable mux session
sshx() { sshx() {
ssh -X -o ControlMaster=no -o ControlPath=none $@ ssh -X -o ControlMaster=no -o ControlPath=none "$@"
} }