This commit is contained in:
Marc Beninca 2024-11-17 22:27:38 +01:00
parent 1084edee23
commit 02a743fc1b
Signed by: marc.beninca
GPG key ID: 9C7613450C80C24F
45 changed files with 0 additions and 0 deletions

3
sh/.shellcheckrc Normal file
View file

@ -0,0 +1,3 @@
disable=1090,3043
enable=all
shell=sh

83
sh/alias/apt.sh Normal file
View file

@ -0,0 +1,83 @@
# show package information
acl() {
apt-cache \
show \
"${@}"
}
# package versions policy
acp() {
apt-cache \
policy \
"${@}"
}
# search package
acs() {
apt-cache \
search \
"${@}"
}
#
agap() {
apt-get \
autopurge \
"${@}"
}
#
agar() {
apt-get \
autoremove \
"${@}"
}
# clean packages cache
agc() {
apt-get \
clean \
"${@}"
}
# upgrade allowing package installation or removal
agfu() {
apt-get \
full-upgrade \
"${@}"
}
# install packages
agi() {
apt-get \
install \
"${@}"
}
#
agp() {
apt-get \
purge \
"${@}"
}
#
agr() {
apt-get \
remove \
"${@}"
}
# update packages catalog
agud() {
apt-get \
update \
"${@}"
}
# upgrade forbidding package installation or removal
agug() {
apt-get \
upgrade \
"${@}"
}

4
sh/alias/batcat.sh Normal file
View file

@ -0,0 +1,4 @@
bat() {
batcat \
"${@}"
}

56
sh/alias/btrfs.sh Normal file
View file

@ -0,0 +1,56 @@
bfdf() {
btrfs \
filesystem \
df \
"${@}"
}
bfdu() {
btrfs \
filesystem \
du \
--summarize \
"${@}"
}
bfu() {
btrfs \
filesystem \
usage \
"${@}"
}
bpg() {
btrfs \
property \
get \
"${@}"
}
bsc() {
btrfs \
subvolume \
create \
"${@}"
}
bsd() {
btrfs \
subvolume \
delete \
"${@}"
}
bss() {
btrfs \
subvolume \
snapshot \
"${@}"
}
bssr() {
btrfs \
subvolume \
snapshot -r \
"${@}"
}

23
sh/alias/byobu.sh Normal file
View file

@ -0,0 +1,23 @@
bb() {
byobu \
"${@}"
}
bba() {
byobu \
attach-session \
"${@}"
}
bbl() {
byobu \
ls \
"${@}"
}
bbnd() {
byobu \
new-session \
-d \
"${@}"
}

13
sh/alias/chmod.sh Normal file
View file

@ -0,0 +1,13 @@
# change mode to directory
cmd() {
chmod \
"755" \
"${@}"
}
# change mode to file
cmf() {
chmod \
"644" \
"${@}"
}

13
sh/alias/chown.sh Normal file
View file

@ -0,0 +1,13 @@
# change owner to root
cor() {
chown \
"0:0" \
"${@}"
}
# change owner to user
cou() {
chown \
"1000:1000" \
"${@}"
}

5
sh/alias/clear.sh Normal file
View file

@ -0,0 +1,5 @@
# clear terminal
c() {
clear \
"${@}"
}

6
sh/alias/cp.sh Normal file
View file

@ -0,0 +1,6 @@
# copy interactively
cpi() {
cp \
--interactive \
"${@}"
}

4
sh/alias/emacs.sh Normal file
View file

@ -0,0 +1,4 @@
em() {
emacs \
"${@}"
}

4
sh/alias/evince.sh Normal file
View file

@ -0,0 +1,4 @@
ev() {
evince \
"${@}"
}

8
sh/alias/grep.sh Normal file
View file

@ -0,0 +1,8 @@
# grep from current directory with regex
g() {
grep \
--directories "recurse" \
--line-number \
--regexp \
"${@}"
}

12
sh/alias/kill.sh Normal file
View file

@ -0,0 +1,12 @@
# kill a process by id
k() {
kill \
"${@}"
}
# force kill a process by id
kf() {
kill \
-9 \
"${@}"
}

12
sh/alias/killall.sh Normal file
View file

@ -0,0 +1,12 @@
# kill all instances of a process by name
ka() {
killall \
"${@}"
}
# force kill all instances of a process by name
kaf() {
killall \
-9 \
"${@}"
}

29
sh/alias/ls.sh Normal file
View file

@ -0,0 +1,29 @@
export LS_COLORS="\
di=0;94\
"
# list current directory’s entries
l() {
ls \
--all \
--color \
-l \
-p \
--time-style "+" \
"${@}"
}
# list timestamps
lt() {
l \
--time-style "+%Y%m%d-%H%M%S%-:::z" \
"${@}"
}
# list timestamps recent last
ltt() {
lt \
--reverse \
-t \
"${@}"
}

27
sh/alias/lsblk.sh Normal file
View file

@ -0,0 +1,27 @@
# list block devices
lb() {
lbo \
"SIZE" \
"TYPE" \
"FSTYPE" \
"LABEL" \
"MOUNTPOINTS"
}
# base arguments
lbb() {
lsblk \
--noempty \
"${@}"
}
# output arguments
lbo() {
local argument
local arguments="NAME"
for argument in "${@}"; do
arguments="${arguments},${argument}"
done
lbb \
--output "${arguments}"
}

12
sh/alias/mkdir.sh Normal file
View file

@ -0,0 +1,12 @@
# make a directory
md() {
mkdir \
"${@}"
}
# make a directory after making its parents
mdp() {
mkdir \
--parents \
"${@}"
}

11
sh/alias/mount.sh Normal file
View file

@ -0,0 +1,11 @@
m() {
mount \
"${@}"
}
# remount read-only medium in read-write
remount() {
mount \
-o "remount,rw" \
"/usr/lib/live/mount/medium"
}

6
sh/alias/mv.sh Normal file
View file

@ -0,0 +1,6 @@
# move interactively
mvi() {
mv \
--interactive \
"${@}"
}

4
sh/alias/nano.sh Normal file
View file

@ -0,0 +1,4 @@
nn() {
nano \
"${@}"
}

5
sh/alias/newsboat.sh Normal file
View file

@ -0,0 +1,5 @@
nb() { news_boat "${@}"; }
news_boat() {
newsboat \
"${@}"
}

12
sh/alias/pass.sh Normal file
View file

@ -0,0 +1,12 @@
# display pass entry’s content
p() {
pass \
"${@}"
}
# copy passphrase into clipboard
pc() {
pass \
--clip \
"${@}"
}

7
sh/alias/ps.sh Normal file
View file

@ -0,0 +1,7 @@
# look for a string in processes names
pg() {
ps \
-A |
grep \
"${@}"
}

15
sh/alias/pwgen.sh Normal file
View file

@ -0,0 +1,15 @@
# generate passwords
pwg() {
pwgen \
-1 \
--num-passwords 1048576 \
--secure \
"${@}"
}
# generate passwords with symbols
pwgs() {
pwg \
--symbols \
"${@}"
}

6
sh/alias/rm.sh Normal file
View file

@ -0,0 +1,6 @@
# remove interactively
rmi() {
rm \
--interactive \
"${@}"
}

31
sh/alias/rsync.sh Normal file
View file

@ -0,0 +1,31 @@
# synchronize
rs() {
rsb \
"${@}"
}
# base arguments
rsb() {
rsync \
--archive \
--no-inc-recursive \
--partial \
--progress \
--verbose \
"${@}"
}
# synchronize and delete after
rsda() {
rsb \
--delete-after \
"${@}"
}
# synchronize and delete before
rsdb() { r_sync_delete_before "${@}"; }
r_sync_delete_before() {
rsb \
--delete-before \
"${@}"
}

27
sh/alias/tar.sh Normal file
View file

@ -0,0 +1,27 @@
tc() {
tv \
--create \
--auto-compress \
--file \
"${@}"
}
tl() {
tv \
--list \
--file \
"${@}"
}
tv() {
tar \
--verbose \
"${@}"
}
tx() {
tv \
--extract \
--file \
"${@}"
}

4
sh/alias/tmux.sh Normal file
View file

@ -0,0 +1,4 @@
tm() {
tmux \
"${@}"
}

10
sh/alias/tree.sh Normal file
View file

@ -0,0 +1,10 @@
t() {
tree \
"${@}"
}
ta() {
tree \
-a \
"${@}"
}

7
sh/btrfs.sh Normal file
View file

@ -0,0 +1,7 @@
bsl() {
if [ -n "${1}" ]; then
btrfs subvolume list "${1}" |
cut --delimiter " " --fields 9 |
sort
fi
}

76
sh/debian.sh Normal file
View file

@ -0,0 +1,76 @@
DEBIAN_CODENAME="$(
grep "VERSION_CODENAME" "/etc/os-release" |
cut --delimiter "=" --fields "2"
)"
apt_clean() {
apt-get \
clean
}
apt_conf_write() {
printf "\
Acquire::AllowInsecureRepositories False;
Acquire::AllowWeakRepositories False;
Acquire::AllowDowngradeToInsecureRepositories False;
Acquire::Check-Valid-Until True;
APT::Install-Recommends False;
APT::Install-Suggests False;
APT::Get::Show-Versions True;
Dir::Etc::SourceParts \"\";
Dpkg::Progress True;
" >"/etc/apt/apt.conf.d/apt.conf"
}
apt_install_backports() {
apt_install_target "${DEBIAN_CODENAME}-backports" "${@}"
}
apt_install_release() {
apt_install_target "${DEBIAN_CODENAME}" "${@}"
}
apt_install_target() {
local target="${1}"
shift
local package
for package in "${@}"; do
log_info
log_info "${package}${target}"
apt-get \
install \
--assume-yes \
--target-release "${target}" \
"${package}"
apt_clean
done
}
apt_sources_write() {
printf "%s" "\
deb https://deb.debian.org/debian \
${DEBIAN_CODENAME} main non-free-firmware contrib non-free
deb https://deb.debian.org/debian \
${DEBIAN_CODENAME}-backports main non-free-firmware contrib non-free
deb https://deb.debian.org/debian \
${DEBIAN_CODENAME}-updates main non-free-firmware contrib non-free
deb https://deb.debian.org/debian-security \
${DEBIAN_CODENAME}-security main non-free-firmware contrib non-free
" >"/etc/apt/sources.list"
}
apt_update() {
apt-get \
update
}
apt_upgrade() {
apt-get \
upgrade \
--assume-yes
apt_clean
}
debian_disable_frontend() {
export DEBIAN_FRONTEND="noninteractive"
}

121
sh/fs.sh Normal file
View file

@ -0,0 +1,121 @@
fs_make_btrfs() {
local device="${1}"
local label="${2}"
local uuid="${3}"
if [ -b "${device}" ]; then
set -- \
--force \
--checksum "sha256"
if [ -n "${label}" ]; then
set -- "${@}" \
--label "${label}"
fi
if [ -n "${uuid}" ]; then
set -- "${@}" \
--uuid "${uuid}"
fi
mkfs.btrfs "${@}" "${device}"
fi
}
fs_make_btrfs_swap() {
local path="${1}"
local size="${2}"
local uuid="${3}"
if [ -n "${path}" ]; then
set -- filesystem mkswapfile
if [ -n "${size}" ]; then
set -- "${@}" \
--size "${size}"
fi
if [ -n "${uuid}" ]; then
set -- "${@}" \
--uuid "${uuid}"
fi
btrfs "${@}" "${path}"
fi
}
fs_make_fat() {
local device="${1}"
local name="${2}"
local volid="${3}"
if [ -b "${device}" ]; then
set -- \
-F 32 \
-S 4096
if [ -n "${name}" ]; then
set -- "${@}" \
-n "${name}"
fi
if [ -n "${volid}" ]; then
set -- "${@}" \
-i "${volid}"
fi
mkfs.fat "${@}" "${device}"
fi
}
fs_raid_create() {
if [ -n "${4}" ]; then
local name="${1}"
local uuid="${2}"
shift 2
mdadm \
--create "/dev/md/${name}" \
--level 0 \
--metadata 1 \
--name "md:${name}" \
--raid-devices ${#} \
--uuid "${uuid}" \
"${@}"
fi
}
fs_wipe() {
local device="${1}"
local buffer="${2}"
local count="${3}"
if [ -b "${device}" ]; then
set -- \
status="progress" \
if="/dev/zero" \
of="${device}"
if [ -n "${buffer}" ]; then
set -- "${@}" \
bs="${buffer}"
fi
if [ -n "${count}" ]; then
set -- "${@}" \
count="${count}"
fi
dd "${@}"
fi
}
luks_format() {
local passphrase="${1}"
local device="${2}"
local label="${3}"
local uuid="${4}"
if [ -b "${device}" ]; then
set -- \
--batch-mode \
--cipher "aes-xts-plain64" \
--hash "sha512" \
--iter-time 4096 \
--key-size 512 \
--pbkdf "argon2id" \
--type "luks2" \
--use-random \
--verbose
if [ -n "${label}" ]; then
set -- "${@}" --label "${label}"
fi
if [ -n "${uuid}" ]; then
set -- "${@}" --uuid "${uuid}"
fi
echo "${passphrase}" |
cryptsetup "${@}" luksFormat "${device}"
fi
}

535
sh/git.sh Normal file
View file

@ -0,0 +1,535 @@
GIT_LOG_FORMAT="\
%C(auto)%h%d
S %C(red)%GS
A %C(green)%an %ae
%C(green)%ai
C %C(blue)%cn %ce
%C(blue)%ci
%B"
# add to index
ga() { git_add "${@}"; }
git_add() {
git \
add \
"${@}"
}
# add all to index
gaa() { git_add_all "${@}"; }
git_add_all() {
git \
add \
--all \
"${@}"
}
# add parts of all to index
gaap() { git_add_all_patch "${@}"; }
git_add_all_patch() {
git \
add \
--all \
--patch \
"${@}"
}
# add parts to index
gap() { git_add_patch "${@}"; }
git_add_patch() {
git \
add \
--patch \
"${@}"
}
# create a branch
gb() { git_branch "${@}"; }
git_branch() {
git \
branch \
"${@}"
}
# delete a branch
gbd() { git_branch_delete "${@}"; }
git_branch_delete() {
git \
branch \
--delete \
"${@}"
}
# force a branch deletion
gbdf() { git_branch_delete_force "${@}"; }
git_branch_delete_force() {
git \
branch \
--delete \
--force \
"${@}"
}
# list branches
gbl() { git_branch_list "${@}"; }
git_branch_list() {
git \
branch \
--all \
--list \
--verbose \
--verbose \
"${@}"
}
# set the link to a remote branch from a local branch
gbsu() { git_branch_set_upstream "${@}"; }
git_branch_set_upstream() {
git \
branch \
--set-upstream-to \
"${@}"
}
# switch to a branch or checkout file(s) from a commit
gc() { git_checkout "${@}"; }
git_checkout() {
git \
checkout \
"${@}"
}
# checkout an orphan branch
gco() { git_checkout_orphan "${@}"; }
git_checkout_orphan() {
git \
checkout \
--orphan \
"${@}"
}
# pick a commit
gcp() { git_cherry_pick "${@}"; }
git_cherry_pick() {
git \
cherry-pick \
"${@}"
}
# abort the commit pick
gcpa() { git_cherry_pick_abort "${@}"; }
git_cherry_pick_abort() {
git \
cherry-pick \
--abort \
"${@}"
}
# continue the commit pick
gcpc() { git_cherry_pick_continue "${@}"; }
git_cherry_pick_continue() {
git \
cherry-pick \
--continue \
"${@}"
}
# clean untracked files
gcf() { git_clean_force "${@}"; }
git_clean_force() {
git \
clean \
-d \
--force \
"${@}"
}
# redo the last commit with a different message
gcam() { git_commit_amend_message "${@}"; }
git_commit_amend_message() {
git \
commit \
--amend \
--message \
"${@}"
}
# make a root commit
gcem() { git_commit_empty_message "${@}"; }
git_commit_empty_message() {
git \
commit \
--allow-empty \
--allow-empty-message \
--message \
"${@}"
}
# commit the index
gcm() { git_commit_message "${@}"; }
git_commit_message() {
git \
commit \
--message \
"${@}"
}
# configure the user email
gcue() { git_config_user_email "${@}"; }
git_config_user_email() {
git \
config \
"user.email" \
"${@}"
}
# configure the user name
gcun() { git_config_user_name "${@}"; }
git_config_user_name() {
git \
config \
"user.name" \
"${@}"
}
# differences from last or between commits
gd() { git_diff "${@}"; }
git_diff() {
git \
diff \
"${@}"
}
# display what is indexed in cache
gdc() { git_diff_cached "${@}"; }
git_diff_cached() {
git \
diff \
--cached \
"${@}"
}
# indexed character-level differences
gdcw() { git_diff_cached_word "${@}"; }
git_diff_cached_word() {
git \
diff \
--cached \
--word-diff-regex "." \
"${@}"
}
# differences via external tool
gdt() { git_diff_tool "${@}"; }
git_diff_tool() {
git \
difftool \
--dir-diff \
"${@}"
}
# character-level differences
gdw() { git_diff_word "${@}"; }
git_diff_word() {
git \
diff \
--word-diff-regex "." \
"${@}"
}
# fetch from the remote repository
gf() { git_fetch "${@}"; }
git_fetch() {
git \
fetch \
--tags \
--verbose \
"${@}"
}
# fetch from remote repository and prune local orphan branches
gfp() { git_fetch_prune "${@}"; }
git_fetch_prune() {
git_fetch \
--prune \
"${@}"
}
# garbage collect all orphan commits
ggc() { git_garbage_collect "${@}"; }
git_garbage_collect() {
git \
reflog \
expire \
--all \
--expire "all" &&
git \
gc \
--aggressive \
--prune="now"
}
# initialize a new repository
gi() { git_init "${@}"; }
git_init() {
git \
init \
"${@}"
}
# initialize a new bare repository
gib() { git_init_bare "${@}"; }
git_init_bare() {
git \
init \
--bare \
"${@}"
}
# log history
gl() { git_log "${@}"; }
git_log() {
git \
log \
--abbrev=8 \
--abbrev-commit \
--format="${GIT_LOG_FORMAT}" \
--graph \
"${@}"
}
# log all history
gla() { git_log_all "${@}"; }
git_log_all() {
git_log \
--all \
"${@}"
}
# log all history with patches
glap() { git_log_all_patch "${@}"; }
git_log_all_patch() {
git_log \
--all \
--patch \
"${@}"
}
# log history with patches
glp() { git_log_patch "${@}"; }
git_log_patch() {
git_log \
--patch \
"${@}"
}
# fast-forward merge to remote branch
gm() { git_merge "${@}"; }
git_merge() {
git \
merge \
--ff-only \
"${@}"
}
# abort the current merge commit
gma() { git_merge_abort "${@}"; }
git_merge_abort() {
git \
merge \
--abort \
"${@}"
}
# do a merge commit
gmc() { git_merge_commit "${@}"; }
git_merge_commit() {
git \
merge \
--no-ff \
--message \
"${@}"
}
# squash a branch and index its modifications
gms() { git_merge_squash "${@}"; }
git_merge_squash() {
git \
merge \
--squash \
"${@}"
}
# merge via external tool
gmt() { git_merge_tool "${@}"; }
git_merge_tool() {
git \
mergetool \
"${@}"
}
# push to the remote repository
gp() { git_push "${@}"; }
git_push() {
git \
push \
--tags \
--verbose \
"${@}"
}
# delete from the remote repository
gpd() { git_push_delete "${@}"; }
git_push_delete() {
git \
push \
--delete \
"${@}"
}
# force the push to the remote repository
gpf() { git_push_force "${@}"; }
git_push_force() {
git_push \
--force \
"${@}"
}
# rebase current branch onto another
grb() { git_re_base "${@}"; }
git_re_base() {
git \
rebase \
"${@}"
}
# abort current rebase
grba() { git_re_base_abort "${@}"; }
git_re_base_abort() {
git \
rebase \
--abort \
"${@}"
}
# continue current rebase
grbc() { git_re_base_continue "${@}"; }
git_re_base_continue() {
git \
rebase \
--continue \
"${@}"
}
# force rebase without fast-forward
grbf() { git_re_base_force "${@}"; }
git_re_base_force() {
git \
rebase \
--force-rebase \
"${@}"
}
# rebase interactively
grbi() { git_re_base_interactive "${@}"; }
git_re_base_interactive() {
git \
rebase \
--interactive \
"${@}"
}
# add a new remote repository
grma() { git_re_mote_add "${@}"; }
git_re_mote_add() {
git \
remote \
add \
"${@}"
}
# list remote repositories
grml() { git_re_mote_list "${@}"; }
git_re_mote_list() {
git \
remote \
--verbose \
"${@}"
}
# set the location of a remote repository
grmsu() { git_re_mote_set_upstream "${@}"; }
git_re_mote_set_upstream() {
git \
remote \
set-url \
"${@}"
}
# show connection to a remote repository
grms() { git_re_mote_show "${@}"; }
git_re_mote_show() {
git \
remote \
show \
"${@}"
}
# remove and add removal to index
grm() { git_re_move "${@}"; }
git_re_move() {
git \
rm \
"${@}"
}
# remove file(s) from index or move current branch pointer
grs() { git_re_set "${@}"; }
git_re_set() {
git \
reset \
"${@}"
}
# wipe modifications or reset current branch to another commit
grsh() { git_re_set_hard "${@}"; }
git_re_set_hard() {
git \
reset \
--hard \
"${@}"
}
# show a commit
gsc() { git_show_commit "${@}"; }
git_show_commit() {
git \
show \
"${@}"
}
# current state of repository
gs() { git_status "${@}"; }
git_status() {
git \
status \
--untracked-files="all" \
"${@}"
}
# tag a commit
gt() { git_tag "${@}"; }
git_tag() {
git \
tag \
"${@}"
}
# delete a tag
gtd() { git_tag_delete "${@}"; }
git_tag_delete() {
git \
tag \
--delete \
"${@}"
}

27
sh/gpg.sh Normal file
View file

@ -0,0 +1,27 @@
# turn gpg agent off
gak() { gpg_agent_kill "${@}"; }
gpg_agent_kill() {
gpgconf \
--kill "gpg-agent"
}
# bind gpg agent to current tty
gau() { gpg_agent_update "${@}"; }
gpg_agent_update() {
gpg-connect-agent \
updatestartuptty \
/bye
}
gpg_ssh() {
local user_id
user_id=$(id --user)
if [ "${user_id}" -ne 0 ]; then
if [ -f "${HOME}/.gnupg/gpg-agent.conf" ]; then
SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
export SSH_AUTH_SOCK
fi
fi
}
gpg_ssh

15
sh/gsettings.sh Normal file
View file

@ -0,0 +1,15 @@
ws() {
local bool
local group="org.gnome.mutter"
local name="workspaces-only-on-primary"
local var="${group}/${name}"
# get
bool="$(gsettings get "${group}" "${name}")"
log_debug "${var}: ${bool}"
# not
bool="$(not "${bool}")"
log_debug "bool: ${bool}"
# set
gsettings set "${group}" "${name}" "${bool}"
log_info "${var}: ${bool}"
}

44
sh/log.sh Normal file
View file

@ -0,0 +1,44 @@
LOG_LEVEL_FATAL=0
LOG_LEVEL_ERROR=1
LOG_LEVEL_WARN=2
LOG_LEVEL_INFO=3
LOG_LEVEL_DEBUG=4
LOG_LEVEL_TRACE=5
LOG_LEVEL=${LOG_LEVEL_INFO}
log_debug() {
if [ "${LOG_LEVEL}" -ge "${LOG_LEVEL_DEBUG}" ]; then
echo "[DEBUG]" "${@}"
fi
}
log_error() {
if [ "${LOG_LEVEL}" -ge "${LOG_LEVEL_ERROR}" ]; then
echo "[ERROR]" "${@}"
fi
}
log_fatal() {
if [ "${LOG_LEVEL}" -ge "${LOG_LEVEL_FATAL}" ]; then
echo "[FATAL]" "${@}"
fi
}
log_info() {
if [ "${LOG_LEVEL}" -ge "${LOG_LEVEL_INFO}" ]; then
echo "${@}"
fi
}
log_trace() {
if [ "${LOG_LEVEL}" -ge "${LOG_LEVEL_TRACE}" ]; then
echo "[TRACE]" "${@}"
fi
}
log_warn() {
if [ "${LOG_LEVEL}" -ge "${LOG_LEVEL_WARN}" ]; then
echo " [WARN]" "${@}"
fi
}

60
sh/main.sh Normal file
View file

@ -0,0 +1,60 @@
[ -n "${ENV}" ] || export ENV="/etc/sh/main.sh"
main_commands() {
local file="${1}"
grep "()" "${file}" |
cut --delimiter "(" --fields 1
}
main_source_directory() {
local path="${1}"
if [ -d "${path}" ]; then
local cmd count ifs module modules
modules="$(find "${path}" \
-type "f" \
-name "*.sh" \
-printf "%P
" | sort)"
ifs="${IFS}"
IFS="
"
count=0
echo
echo ". ${path}"
for module in ${modules}; do
count=$((count + 1))
printf "%02d" "${count}"
echo " ${module%.sh}"
module="${path}/${module}"
if [ "${module}" != "${ENV}" ]; then
. "${module}"
cmd="$(main_commands "${module}")"
if [ -n "${cmd}" ]; then
[ -n "${CMD}" ] && CMD="${CMD}
"
CMD="${CMD}${cmd}"
fi
fi
done
IFS="${ifs}"
log_info
log_info "${CMD}"
else
echo "Not a directory: ${path}"
return 1
fi
}
main_source_file() {
local path="${1}"
if [ -f "${path}" ]; then
main_source_directory "$(dirname "${path}")"
else
echo "Not a file: ${path}"
return 1
fi
}
main_source_file "${ENV}"
main_source_directory "${HOME}/shell"

31
sh/mount-lxc.sh Normal file
View file

@ -0,0 +1,31 @@
mrc() {
local container="${1}"
local f
for f in "dev" "dev/pts" "proc" "sys"; do
mount --bind "/${f}" "overlay/mount/var/lib/lxc/${container}/squashfs-root/${f}"
done
}
crc() {
local container="${1}"
shift
chroot "overlay/mount/var/lib/lxc/${container}/squashfs-root" "${@}"
}
urc() {
local container="${1}"
local f
for f in "sys" "proc" "dev/pts" "dev"; do
umount --lazy "overlay/mount/var/lib/lxc/${container}/squashfs-root/${f}"
done
}
mmc() {
local container="${1}"
mount --bind "/deb" "overlay/mount/var/lib/lxc/${container}/squashfs-root/deb"
}
umc() {
local container="${1}"
umount "overlay/mount/var/lib/lxc/${container}/squashfs-root/deb"
}

131
sh/mount.sh Normal file
View file

@ -0,0 +1,131 @@
mo() {
local root="${1}"
if [ -z "${root}" ]; then
log_error "No root target directory"
return 1
fi
root="$(realpath "${root}")"
if ! mkdir "overlay"; then
log_error "Unable to make overlay directory"
return 2
fi
(
if ! cd "overlay"; then
log_error "Unable to move into overlay directory"
return 3
fi
local directory
for directory in "lower" "upper" "work" "mount"; do
if ! mkdir --parents "${directory}"; then
log_error "Unable to make directory: ${directory}"
return 4
fi
done
local file="${root}/filesystem.squashfs"
if ! mount "${file}" "lower"; then
log_error "Unable to lower mount: ${file}"
return 5
fi
if ! mount \
-o "lowerdir=lower,upperdir=upper,workdir=work" \
-t "overlay" \
"overlay" "mount"; then
log_error "Unable to overlay mount"
return 6
fi
)
}
uo() {
(
if ! cd "overlay"; then
log_error "Unable to move into overlay directory"
return 1
fi
if ! umount "mount"; then
log_error "Unable to unmount mount directory"
return 2
fi
if ! rmdir "mount"; then
log_error "Unable to remove mount directory"
return 3
fi
local directory
for directory in "upper" "work"; do
if ! rm --force --recursive "${directory}"; then
log_error "Unable to remove directory: ${directory}"
return 4
fi
done
if ! umount "lower"; then
log_error "Unable to unmount lower directory"
return 5
fi
if ! rmdir "lower"; then
log_error "Unable to remove lower directory"
return 6
fi
)
if ! rmdir "overlay"; then
log_error "Unable to remove overlay directory"
return 7
fi
}
mr() {
local directory
for directory in "dev" "dev/pts" "proc" "sys"; do
if ! mount --bind "/${directory}" "overlay/mount/${directory}"; then
log_error "Unable to bind mount directory: ${directory}"
return 1
fi
done
}
cr() {
chroot \
"overlay/mount" "${@}"
}
cru() {
chroot \
--userspec "1000:1000" \
"overlay/mount" "${@}"
}
ur() {
local directory
for directory in "sys" "proc" "dev/pts" "dev"; do
if ! umount --lazy "overlay/mount/${directory}"; then
log_error "Unable to bind unmount directory: ${directory}"
return 1
fi
done
}
mm() {
mount --make-rslave --rbind "/deb" "overlay/mount/deb"
}
um() {
umount --recursive "overlay/mount/deb"
}
ms() {
local directory="${1}"
local file
local level="${2}"
if [ -n "${directory}" ]; then
if mkdir "${directory}"; then
[ -n "${level}" ] || level="18"
for file in "vmlinuz" "initrd.img"; do
cp "overlay/mount/${file}" "${directory}"
done
mksquashfs \
"overlay/mount" "${directory}/filesystem.squashfs" \
-noappend \
-comp "zstd" -Xcompression-level "${level}"
chown --recursive 1000:1000 "${directory}"
fi
fi
}

8
sh/proxy.sh Normal file
View file

@ -0,0 +1,8 @@
socks() {
local value
case "${1}" in
"on") value="manual" ;;
*) value="none" ;;
esac
gsettings set "org.gnome.system.proxy" "mode" "${value}"
}

94
sh/rescue/common.sh Normal file
View file

@ -0,0 +1,94 @@
rescue_configure() {
local hostname="${1}"
# apt / conf
apt_conf_write
# apt / sources
apt_sources_write
# bash / rc
main_link_bashrc
mv .bashrc .bashrc.old
# host name
hostname "${hostname}"
# locales
printf "\
en_US.UTF-8 UTF-8
fr_FR.UTF-8 UTF-8
" >"/etc/locale.gen"
# generate locales
locale-gen
# update catalog
apt_update
# disable frontend
debian_disable_frontend
# install backports
apt_install_backports "tmux"
# install packages
apt_install_release "apt-file" "mosh" "screen" "byobu"
# update catalog
apt_update
}
rescue_install() {
# update catalog
apt_update
# disable frontend
debian_disable_frontend
# upgrade packages
apt_upgrade
# install packages
apt_install_release \
"man-db" \
"dmidecode" "efibootmgr" "lshw" "pciutils" "usbutils" \
"parted" "mdadm" "cryptsetup-bin" "lvm2" \
"btrfs-progs" "dosfstools" "duperemove" "squashfs-tools" \
"git" "micro" "nano" "python3" "rsync" "vim" \
"exa" "lf" "ncdu" "nnn" "ranger" "tree" \
"file" "htop" "iotop" "ipcalc" "libdigest-sha3-perl" "lsof"
# install backports
apt_install_backports \
"grub-pc-bin" \
\
"grub-efi-amd64-bin"
}
rescue_upload() {
local host="${1}"
local hostname="${2}"
if [ -n "${hostname}" ]; then
local user="root"
#
local user_host="${user}@${host}"
# remove fingerprints
ssh-keygen -R "${host}"
# copy ssh id
ssh-copy-id \
-o "StrictHostKeyChecking=accept-new" \
"${user_host}"
# upload root
rsync --delete --recursive \
"$(dirname "${ENV}")" "${user_host}:/etc"
# call setup
# TODO variable
ssh "${user_host}" -- \
". \"${ENV}\" ; rescue_configure \"${hostname}\""
# create session
ssh "${user_host}" -- byobu new-session -d
# send keys
ssh "${user_host}" -- byobu send-keys "rescue_install" "C-m"
# attach session
mosh "${user_host}" -- byobu attach-session
else
echo "host & hostname"
return 1
fi
}
rescue_wipe_1_zero() {
fs_wipe "/dev/mapper/crypt" "512M"
}
rescue_wipe_3_close() {
umount "/media/boot"
umount "/media/crypt" &&
cryptsetup luksClose "crypt"
}

127
sh/rescue/hetzner.sh Normal file
View file

@ -0,0 +1,127 @@
rescue_wipe_0_init_hetzner_8_8() {
local device
set \
"/dev/sda" \
"/dev/sdb"
local members
local number
local passphrase
# read passphrase
passphrase="$(read_passphrase)"
# warn
warn_wipe "${@}"
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}"
#
parted --script "${device}" \
mktable gpt \
unit "mib" \
mkpart "crypt-${number}" 33282 7630885 \
mkpart "boot-${number}" 514 33282 \
mkpart "esp-${number}" 2 514 \
set 3 esp on \
mkpart "bios-${number}" 1 2 \
set 4 bios_grub on
done
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}4"
# wipe bios
fs_wipe "${device}4"
done
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}3"
# format esp
fs_wipe "${device}3" "1M"
fs_make_fat "${device}3" "esp-${number}" "0000000${number}"
# mount esp
mkdir --parents "/media/esp/${number}"
mount "${device}3" "/media/esp/${number}"
done
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}2"
# wipe boot
fs_wipe "${device}2" "1G" 1
done
#
members=""
for device in "${@}"; do
members="${members} ${device}2"
done
# shellcheck disable=SC2086
fs_raid_create \
"boot" "00000000:00000000:00000000:00000002" ${members}
#
fs_make_btrfs "/dev/md/boot" "boot" \
"00000000-0000-0000-0000-00000000000b"
# mount boot
mkdir --parents "/media/boot"
mount \
--options "autodefrag,compress-force=zstd" \
"/dev/md/boot" "/media/boot"
#
number=0
for device in "${@}"; do
number=$((number + 1))
echo
echo "#${number}: ${device}1"
# wipe crypt head
fs_wipe "${device}1" "1G" 1
done
#
members=""
for device in "${@}"; do
members="${members} ${device}1"
done
# shellcheck disable=SC2086
fs_raid_create \
"crypt" "00000000:00000000:00000000:00000001" ${members}
# encrypt
luks_format "${passphrase}" "/dev/md/crypt"
# open
echo "${passphrase}" |
cryptsetup luksOpen "/dev/md/crypt" "crypt"
# passphrase
unset passphrase
}
rescue_wipe_2_make_hetzner_8_8() {
local passphrase
# close
cryptsetup luksClose "crypt"
# read passphrase
passphrase="$(read_passphrase)"
# encrypt
luks_format "${passphrase}" "/dev/md/crypt"
# open
echo "${passphrase}" |
cryptsetup luksOpen "/dev/md/crypt" "crypt"
# passphrase
unset passphrase
# format crypt
fs_make_btrfs "/dev/mapper/crypt" "crypt" \
"00000000-0000-0000-0000-00000000000c"
# mount crypt
mkdir --parents "/media/crypt"
mount \
--options "autodefrag,compress-force=zstd" \
"/dev/mapper/crypt" "/media/crypt"
# make swap file
fs_make_btrfs_swap "/media/crypt/swap" "64g" \
"00000000-0000-0000-0000-000000000005"
}

71
sh/rescue/ovh.sh Normal file
View file

@ -0,0 +1,71 @@
rescue_wipe_0_init_ovh_vle2() {
local device="/dev/sdb"
local passphrase
# read passphrase
passphrase="$(read_passphrase)"
# warn
warn_wipe "${device}"
#
parted --script "${device}" \
mktable gpt \
unit "mib" \
mkpart "crypt" 4610 40959 \
mkpart "boot" 514 4610 \
mkpart "esp" 2 514 \
set 3 esp on \
mkpart bios 1 2 \
set 4 bios_grub on
# bios / wipe
fs_wipe "${device}4"
# esp / wipe
fs_wipe "${device}3" "1M"
# esp / format
fs_make_fat "${device}3" "esp" "00000001"
# esp / mount
mkdir --parents "/media/esp"
mount "${device}3" "/media/esp"
# boot / wipe
fs_wipe "${device}2" "1G" 1
# boot / format
fs_make_btrfs "${device}2" "boot" \
"00000000-0000-0000-0000-00000000000b"
# boot / mount
mkdir --parents "/media/boot"
mount --options "autodefrag,compress-force=zstd" \
"${device}2" "/media/boot"
# crypt / wipe
fs_wipe "${device}1" "1G" 1
# crypt / encrypt
luks_format "${passphrase}" "${device}1"
# crypt / open
echo "${passphrase}" |
cryptsetup luksOpen "${device}1" "crypt"
# passphrase
unset passphrase
}
rescue_wipe_2_make_ovh_vle2() {
local device="/dev/sdb"
local passphrase
# crypt / close
cryptsetup luksClose "crypt"
# read passphrase
passphrase="$(read_passphrase)"
# crypt / encrypt
luks_format "${passphrase}" "${device}1"
# crypt / open
echo "${passphrase}" |
cryptsetup luksOpen "${device}1" "crypt"
# passphrase
unset passphrase
# crypt / format
fs_make_btrfs "/dev/mapper/crypt" "crypt" \
"00000000-0000-0000-0000-00000000000c"
# crypt / mount
mkdir --parents "/media/crypt"
mount --options "autodefrag,compress-force=zstd" \
"/dev/mapper/crypt" "/media/crypt"
# crypt / swap
fs_make_btrfs_swap "/media/crypt/swap" "4g" \
"00000000-0000-0000-0000-000000000005"
}

152
sh/shell.sh Normal file
View file

@ -0,0 +1,152 @@
PS1="\$(shell_prompt \${?})"
PS2="\
"
SH="$(cat /proc/$$/comm)"
shell_color() {
local code="${1}"
case "${SH}" in
"bash")
printf "\x01\e[0"
if [ -n "${code}" ]; then
printf "%s" ";${code}"
fi
printf "m\x02"
;;
*)
printf "\033["
if [ -n "${code}" ]; then
printf "%s" "${code}"
else
printf "0"
fi
printf "m"
;;
esac
}
SH_BROWN="$(shell_color 33)"
SH_CYAN="$(shell_color 36)"
SH_DEFAULT="$(shell_color)"
SH_GREEN="$(shell_color 31)"
SH_MAGENTA="$(shell_color 35)"
SH_RED="$(shell_color 32)"
shell_configure() {
case "${SH}" in
"bash")
# completion
local root="/usr/share/bash-completion"
local file="bash_completion"
local path="${root}/${file}"
[ -f "${path}" ] && . "${path}"
root="${root}/completions"
if [ -d "${root}" ]; then
set \
"git" \
"tar"
for file in "${@}"; do
path="${root}/${file}"
[ -f "${path}" ] && . "${path}"
done
fi
# history
HISTCONTROL="ignorespace"
HISTSIZE=-1
HISTTIMEFORMAT="%Y%m%d %H%M%S "
;;
*) ;;
esac
}
shell_configure
shell_prompt() {
local date host id
local code="${1}"
date="$(date +%H:%M:%S)"
local git
host="$(hostname)"
id="$(id --user)"
local path="${PWD}"
local user="${USER}"
local view="└ "
# code
if [ "${code}" -ne 0 ]; then
view="${view}${SH_GREEN}"
else
view="${view}${SH_RED}"
fi
view="${view}${code}"
# date
view="${view}${SH_DEFAULT} @ "
view="${view}${SH_BROWN}${date}"
# git
if command -v "__git_ps1" >"/dev/null"; then
git="$(__git_ps1)"
if [ -n "${git}" ]; then
view="${view}${SH_DEFAULT}${SH_MAGENTA}${git}"
fi
fi
# new
view="${view}\\n"
# path
view="${view}${SH_CYAN}${path}"
# new
view="${view}\\n"
# frame
view="${view}${SH_DEFAULT}"
# user
if [ "${id}" -eq 0 ]; then
view="${view}${SH_GREEN}"
else
view="${view}${SH_RED}"
fi
view="${view}${user}"
# host
view="${view}${SH_DEFAULT} @ "
view="${view}${SH_BROWN}${host}"
# new
view="${view}\\n"
# prompt
view="${view}${SH_DEFAULT}${PS2}"
# print
printf "%b" "${view}"
}
shell_setup() {
# shell
echo "export ENV=\"${ENV}\"" >"/etc/profile.d/shell.sh"
# bash
local file="/etc/bash.bashrc"
rm --force --recursive "${file}"
ln --symbolic "${ENV}" "${file}"
}
# shorten alias
a() {
alias \
"${@}"
}
# swap directory (current ↔ previous)
sd() {
cd \
- ||
return
}
# exit terminal
x() {
exit \
"${@}"
}
[ "${SH}" = "bash" ] || return
# shellcheck disable=SC3033
..() {
cd ..
}
# shellcheck disable=SC3033
...() {
cd ../..
}

37
sh/util.sh Normal file
View file

@ -0,0 +1,37 @@
list_block_devices() {
lsblk \
--noempty \
--output "NAME,SIZE,TYPE,FSTYPE,LABEL,MOUNTPOINTS"
}
not() {
case "${1}" in
"false") echo "true" ;;
"true") echo "false" ;;
*) ;;
esac
}
read_passphrase() {
read_secret "PassPhrase: "
}
read_secret() {
local prompt="${1}"
local secret
printf "%s" "${prompt}" 1>&2
stty -echo
read -r secret
stty echo
echo >&2
echo "${secret}"
unset secret
}
warn_wipe() {
local tmp
list_block_devices
printf "%s" "WIPE ${*} /?\\ OR CANCEL /!\\"
read -r tmp
log_trace "${tmp}"
}