Compare commits
37 Commits
v3.6.1
...
ac66e896dd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac66e896dd | ||
|
|
0712be626b | ||
|
|
3f8b81562b | ||
|
|
96d1dc695d | ||
|
|
c039ab6ea0 | ||
|
|
6b85556a53 | ||
|
|
cf9a85e61b | ||
|
|
75e047d70e | ||
|
|
742ec484a7 | ||
|
|
1b7262c0cd | ||
|
|
e387209c10 | ||
|
|
f5d59ec194 | ||
|
|
2ee1c935ac | ||
|
|
e41c1a4c51 | ||
|
|
60dfe19049 | ||
|
|
c32771a4ff | ||
|
|
080511d0bd | ||
|
|
d8bdfefdf1 | ||
|
|
5f5f9c0e71 | ||
|
|
30387a4f08 | ||
|
|
0c51363d86 | ||
|
|
043fbaef0b | ||
|
|
ed5587712e | ||
|
|
58cc76c317 | ||
|
|
e82ee06e1d | ||
|
|
bc8cb4a237 | ||
|
|
ae90a9f4c4 | ||
|
|
7e661ca2de | ||
|
|
9f22ed4304 | ||
|
|
1484b004be | ||
|
|
0a4206b890 | ||
|
|
02a1e25df2 | ||
|
|
7ca0a6fb88 | ||
|
|
25df408e37 | ||
|
|
3eab1f98d5 | ||
|
|
6c895b509a | ||
|
|
2ece711e1a |
@@ -20,7 +20,8 @@ prompt. Here is a non-exhaustive list of what we have:
|
||||
- A bar style prompt with hour, execution time and exit code of the last
|
||||
command;
|
||||
- clean: erase after confirmation any backup file, possibly recursively;
|
||||
- dpkgs: search for the given pattern in the installed packages name;
|
||||
- dwl: a curl/wget/fetch download wrapper;
|
||||
- pkgs: search for the given pattern in the installed packages name;
|
||||
- expandlist: usefull in scripts, it expand any expression using wildcards into
|
||||
the corresponding list of file and directories;
|
||||
- genpwd: generate one or more random secure password;
|
||||
@@ -31,6 +32,7 @@ the corresponding list of file and directories;
|
||||
- ku: kill all the processes owned by the given user name or ID;
|
||||
- mcd: create a directory and immediately move into it;
|
||||
- meteo: display weather forecast information;
|
||||
- myextip: get informations about your public IP;
|
||||
- ppg: look for the given patern in the running processes;
|
||||
- rain: console screensaver with rain effect;
|
||||
- rmhost: remove the given host (name or IP) to the list of SSH known host;
|
||||
@@ -51,8 +53,9 @@ directory only if needed;
|
||||
|
||||
## 3. Configuration
|
||||
Some functions might have configurable default behaviour. You can create a
|
||||
.profile.conf file to configure those default behaviour. You should have a look
|
||||
at the doc/.profile.conf.example to see the list of available options.
|
||||
profile.conf file to configure those default behaviour. You should have a look
|
||||
at the doc/profile.conf.example to see the list of available options. The
|
||||
configuration file is located in the same directory as profile.sh file.
|
||||
|
||||
## 4. Contact and more information
|
||||
### 4.1. New users
|
||||
|
||||
110
profile.conf
Executable file
110
profile.conf
Executable file
@@ -0,0 +1,110 @@
|
||||
[system]
|
||||
# System section is used to set Bash behavior and other system related
|
||||
# variables, such as the default pager, the terminal type, etc.
|
||||
# Set bash history
|
||||
HISTSIZE=50000
|
||||
HISTIGNORE="&:[bf]g:exit"
|
||||
|
||||
# Set default pager
|
||||
PAGER=less
|
||||
|
||||
# Set terminal colors behavior
|
||||
TERM=xterm-256color
|
||||
|
||||
[compress]
|
||||
# Section used by compress.sh
|
||||
|
||||
[debug]
|
||||
# Section used by debug.sh
|
||||
|
||||
[disp]
|
||||
# Section used by disp.sh
|
||||
# Set to any value to disable colors in internal profile output (not controling binary output)
|
||||
# NO_COLOR=1
|
||||
|
||||
[filefct]
|
||||
# Section used by filefct.sh
|
||||
|
||||
[fun]
|
||||
# Section used by fun.sh
|
||||
|
||||
[info]
|
||||
# Section used by info.sh
|
||||
# Default city for weather forcast and local news
|
||||
DEFAULT_CITY="Toulouse"
|
||||
|
||||
[lang]
|
||||
# Section used by lang.sh
|
||||
# List of locale shortcuts to build, in the form "shortcut:locale,...".
|
||||
# Generate a function setXX for each shortcut defined.
|
||||
SET_LOCALE="fr:fr_FR.UTF-8,us:en_US.UTF-8"
|
||||
|
||||
[net]
|
||||
# Section used by net.sh
|
||||
|
||||
[packages]
|
||||
# Section used by packages.sh
|
||||
|
||||
[prompt]
|
||||
# Section used by prompt.sh
|
||||
|
||||
[pwd]
|
||||
# Section used by pwd.sh
|
||||
|
||||
[rain]
|
||||
# Section used by rain.sh
|
||||
|
||||
[ssh]
|
||||
# Section used by ssh.sh
|
||||
|
||||
[updates]
|
||||
# Section used by updates.sh
|
||||
|
||||
[general]
|
||||
# General section allow to set any variable that can be used by the user.
|
||||
# It is also a good place to set freely global variables for personal use.
|
||||
# Set some compiling values
|
||||
CFLAGS="-O2 -pipe -march=native"
|
||||
CXXFLAGS="$CFLAGS"
|
||||
MAKEFLAGS='-j12'
|
||||
PKGSOURCES='/share/src/archives'
|
||||
|
||||
[aliases]
|
||||
# Aliases section is used to set user aliases, it is loaded only for
|
||||
# interactive shells.
|
||||
# Various ls aliases
|
||||
ll='ls -laFh --color=auto'
|
||||
la='ls -Ah --color=auto'
|
||||
l='ls -CF --color=auto'
|
||||
ls='ls --color=auto'
|
||||
|
||||
# Add color to grep output
|
||||
grep='grep --color=auto'
|
||||
egrep='egrep --color=auto'
|
||||
fgrep='fgrep --color=auto'
|
||||
|
||||
# Quick find alias
|
||||
qfind="find . -name "
|
||||
|
||||
# Some alias for compiling
|
||||
mk='make'
|
||||
mkck='make check'
|
||||
mkin='make install'
|
||||
mkdin='make DESTDIR=$PWD/dest-install install'
|
||||
|
||||
# ssh alias with X11 forwarding, without right restriction
|
||||
ssh='ssh -Y'
|
||||
|
||||
# Resume mode for wget
|
||||
wget='wget -c' # resume mode by default
|
||||
|
||||
# Human readable by default
|
||||
df='df -H'
|
||||
du='du -ch'
|
||||
sdu='du -sk ./* | sort -n'
|
||||
hdu='du -hs ./* | sort -H'
|
||||
|
||||
# Readable dmesg timestamps
|
||||
dmesg='dmesg -T'
|
||||
|
||||
# End of profile.conf
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -35,8 +35,13 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Smartly uncompress archives (zip only for now)
|
||||
# ------------------------------------------------------------------------------
|
||||
# Smartly uncompress archives
|
||||
# Usage: utaz [option] [directorie(s)|file(s)]
|
||||
# Options:
|
||||
# -h, --help Display that help screen
|
||||
# -d, --delete If decompression succeeded, delete the source file
|
||||
# -c, --create-dir Always create a host directory
|
||||
# -n, --no-dir Never create a host directory
|
||||
utaz()
|
||||
{
|
||||
_ununzip()
|
||||
@@ -136,54 +141,89 @@ utaz()
|
||||
rpm2cpio "$1" | (cd "$2/" && cpio -idmv) >/dev/null 2>&1
|
||||
}
|
||||
|
||||
for opt in $@; do
|
||||
case ${opt} in
|
||||
"-h" | "--help")
|
||||
echo "utaz: uncompress all the given files and/or the ones found in the given"
|
||||
echo " directories creating an host directory where needed."
|
||||
echo
|
||||
echo "Usage: utaz [option] [directorie(s)|file(s)]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -h, --help Display that help screen"
|
||||
echo " -d, --delete If decompression succeeded, delete the source file"
|
||||
echo " -c, --create-dir Always create a host directory"
|
||||
echo " -n, --no-dir Never create a host directory"
|
||||
echo
|
||||
local PARSED=$(getopt -o hdcn --long help,delete,create-dir,no-dir -n 'utaz' -- "$@")
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
disp E "Invalid options, use \"utaz --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "utaz: uncompress all the given files and/or the ones found in the given\n"
|
||||
printf " directories creating an host directory where needed.\n\n"
|
||||
printf "Usage: utaz [option] [directorie(s)|file(s)]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay that help screen\n"
|
||||
printf "\t-d, --delete\t\tIf decompression succeeded, delete the source file\n"
|
||||
printf "\t-c, --create-dir\tAlways create a host directory\n"
|
||||
printf "\t-n, --no-dir\t\tNever create a host directory\n\n"
|
||||
printf "Supported archive format:\n"
|
||||
printf "\t- zip\n"
|
||||
printf "\t- tar.gz, .tgz\n"
|
||||
printf "\t- tar.bz2, .tbz2\n"
|
||||
printf "\t- tar.xz, .txz\n"
|
||||
printf "\t- tar.lz, .tlz\n"
|
||||
printf "\t- rar\n"
|
||||
printf "\t- arj\n"
|
||||
printf "\t- lha, lzh\n"
|
||||
printf "\t- ace\n"
|
||||
printf "\t- 7z, p7z\n"
|
||||
printf "\t- zst\n"
|
||||
printf "\t- cpio\n"
|
||||
printf "\t- cab\n"
|
||||
printf "\t- deb\n"
|
||||
printf "\t- rpm\n"
|
||||
return 0
|
||||
;;
|
||||
|
||||
"-d" | "--delete")
|
||||
-d|--delete)
|
||||
local willrm=1
|
||||
shift
|
||||
;;
|
||||
|
||||
"-c" | "--create-dir")
|
||||
-c|--create-dir)
|
||||
local createdir=1
|
||||
shift
|
||||
;;
|
||||
|
||||
"-n" | "--no-dir")
|
||||
-n|--no-dir)
|
||||
local nodir=1
|
||||
shift
|
||||
;;
|
||||
|
||||
"-"*)
|
||||
disp E "Invalid option, use \"utaz --help\" to display options list"
|
||||
echo
|
||||
return 1
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
|
||||
*)
|
||||
# The ${opt%/} writing is to remove trailing / if any
|
||||
local LIST="${LIST} ${opt%/}"
|
||||
disp E "Invalid option, use \"utaz --help\" to display options list"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
# The remaining arguments after -- are the files/directories to process,
|
||||
# "." is used if none is given
|
||||
local FILES=("$@")
|
||||
[[ ${#FILES[@]} -eq 0 ]] && FILES=(".")
|
||||
|
||||
[[ -n ${createdir} && -n ${nodir} ]] && \
|
||||
disp E "The --create-dir and --no-dir options are mutually exclusive."
|
||||
|
||||
[[ -z ${LIST} ]] && local LIST="."
|
||||
for zitem in ${LIST}; do
|
||||
for f in "${zitem}"/*; do
|
||||
for zitem in "${FILES[@]}"; do
|
||||
# Build list of input files to process, with whitespace-safe handling.
|
||||
local targets=()
|
||||
if [[ -f "$zitem" ]]; then
|
||||
targets=("$zitem")
|
||||
elif [[ -d "$zitem" ]]; then
|
||||
mapfile -d '' -t targets < <(find "$zitem" -mindepth 1 -maxdepth 1 -print0 2>/dev/null)
|
||||
if [[ ${#targets[@]} -eq 0 ]]; then
|
||||
disp I "Directory ${zitem} is empty, skipping."
|
||||
continue
|
||||
fi
|
||||
else
|
||||
disp W "Path ${zitem} is not a file or directory, skipping."
|
||||
continue
|
||||
fi
|
||||
|
||||
for f in "${targets[@]}"; do
|
||||
local dir="${f%.*}"
|
||||
local extractor=""
|
||||
case "$f" in
|
||||
@@ -193,13 +233,13 @@ utaz()
|
||||
*.tar.gz|*.tgz)
|
||||
extractor="_ungzip"
|
||||
;;
|
||||
*.tar.bz2)
|
||||
*.tar.bz2|*.tbz2)
|
||||
extractor="_unbzip2"
|
||||
;;
|
||||
*.tar.xz)
|
||||
*.tar.xz|*.txz)
|
||||
extractor="_unxz"
|
||||
;;
|
||||
*.tar.lz)
|
||||
*.tar.lz|*.tlz)
|
||||
extractor="_unlzop"
|
||||
;;
|
||||
*.tar)
|
||||
@@ -217,7 +257,7 @@ utaz()
|
||||
*.ace)
|
||||
extractor="_ununace"
|
||||
;;
|
||||
*.7z)
|
||||
*.7z|*.p7z)
|
||||
extractor="_un7z"
|
||||
;;
|
||||
*.zst)
|
||||
@@ -244,21 +284,21 @@ utaz()
|
||||
# Verify binary existence
|
||||
local cmd=${extractor//_un/}
|
||||
if [[ $cmd == "deb" ]]; then
|
||||
command -v dpkg-deb >/dev/null 2>&1 || {
|
||||
command -v -- dpkg-deb >/dev/null 2>&1 || {
|
||||
disp E "The program 'dpkg-deb' is not installed, aborting."
|
||||
continue
|
||||
}
|
||||
elif [[ $cmd == "rpm" ]]; then
|
||||
command -v rpm2cpio >/dev/null 2>&1 || {
|
||||
command -v -- rpm2cpio >/dev/null 2>&1 || {
|
||||
disp E "The program 'rpm2cpio' is not installed, aborting."
|
||||
continue
|
||||
}
|
||||
command -v cpio >/dev/null 2>&1 || {
|
||||
command -v -- cpio >/dev/null 2>&1 || {
|
||||
disp E "The program 'cpio' is not installed, aborting."
|
||||
continue
|
||||
}
|
||||
else
|
||||
command -v ${cmd} >/dev/null 2>&1 || {
|
||||
command -v -- "${cmd}" >/dev/null 2>&1 || {
|
||||
disp E "Binary ${cmd} necessary to extract ${f} is missing."
|
||||
continue
|
||||
}
|
||||
@@ -277,10 +317,14 @@ utaz()
|
||||
rm -f "${f}" && disp I "File ${zitem}/${f} deleted."
|
||||
;;
|
||||
1)
|
||||
if [[ -n ${willrm} ]]; then
|
||||
disp W "Compression program returned a warning: deletion canceled."
|
||||
else
|
||||
disp W "Compression program returned a warning."
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
disp E "The zip file seems corrupted, failed."
|
||||
disp E "The compressed file ${f} seems corrupted, failed."
|
||||
rm -rf "${dir}" >/dev/null 2>&1
|
||||
continue
|
||||
;;
|
||||
@@ -289,7 +333,12 @@ utaz()
|
||||
if [[ -n ${createdir} ]]; then
|
||||
disp I "Archive extracted successfully in subdirectory."
|
||||
elif [[ -n ${nodir} ]]; then
|
||||
mv "./${dir}/"* ./ && rmdir "${dir}"
|
||||
shopt -s nullglob
|
||||
for child in "${dir}"/*; do
|
||||
mv -- "$child" .
|
||||
done
|
||||
shopt -u nullglob
|
||||
rmdir -- "${dir}"
|
||||
disp I "Archive extracted successfully, no subdirectory needed."
|
||||
else
|
||||
# Set nullglob to ensure the array is empty if no files match
|
||||
@@ -299,7 +348,12 @@ utaz()
|
||||
# Check if exactly one item exists and if that item is a directory
|
||||
if [[ ${#contents[@]} -eq 1 ]] && [[ -d "${contents[0]}" ]]; then
|
||||
# Single directory detected
|
||||
mv "${contents[0]}"/* ./ && rmdir "${dir}"
|
||||
shopt -s nullglob
|
||||
for child in "${contents[0]}"/*; do
|
||||
mv -- "$child" .
|
||||
done
|
||||
shopt -u nullglob
|
||||
rmdir -- "${dir}"
|
||||
disp I "Archive extracted successfully, no subdirectory needed."
|
||||
else
|
||||
disp I "Archive extracted successfully in subdirectory."
|
||||
@@ -310,10 +364,21 @@ utaz()
|
||||
done
|
||||
}
|
||||
export -f utaz
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Compress directories or files into one or more archive
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: taz [option] [--parallel=<n>] [--format=<format>] [directory1 ... directoryN]
|
||||
# Options:
|
||||
# -h, --help Display that help screen
|
||||
# -d, --delete Delete source file or directory after success
|
||||
# -f, --format Chose archive format in the given list. If several format are
|
||||
# given, the smalest is kept
|
||||
# -p, --parallel Number of threads to use (if allowed by underlying utility)
|
||||
# -v, --verbose Display progress where possible
|
||||
# -q, --quiet Display less messages (only errors and warnings)
|
||||
# -1, .., -9 Compression level to use [1=fast/biggest, 9=slow/smallest]
|
||||
taz()
|
||||
{
|
||||
_doxz()
|
||||
@@ -421,78 +486,92 @@ taz()
|
||||
return $?
|
||||
}
|
||||
|
||||
for opt in $@; do
|
||||
case $opt in
|
||||
"-h" | "--help")
|
||||
echo "taz: archive all files of a directory."
|
||||
echo
|
||||
echo "Usage: taz [option] [--parallel=<n>] [--format=<format>] [directory1 ... directoryN]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -h, --help Display that help screen"
|
||||
echo " -d, --delete Delete source file or directory after success"
|
||||
echo " -f, --format Chose archive format in the given list. If several format are"
|
||||
echo " given, the smalest is kept"
|
||||
echo " -p, --parallel Number of threads to use (if allowed by underlying utility)"
|
||||
echo " -v, --verbose Display progress where possible"
|
||||
echo " -q, --quiet Display less messages (only errors and warnings)"
|
||||
echo " -1, .., -9 Compression level to use [1=fast/biggest, 9=slow/smallest]"
|
||||
echo
|
||||
echo "Supported archive format:"
|
||||
echo " Param.| programs | Algo. | Description"
|
||||
echo " ------+---------------+-------+----------------------------------------"
|
||||
echo " lz | plzip, lzip | lzma | Safe efficient default format"
|
||||
echo " xz | xz | lzma2 | Unsafe, not for long term"
|
||||
echo " bz2 | pbzip2, bzip2 | bzip2 | Historical but less efficient than lz"
|
||||
echo " gz | pigz, gzip | lz77 | Historical, safe, fast"
|
||||
echo " lzo | lzop | lzo | Very fast but no multithread"
|
||||
echo " tar | tar | tar | No compression"
|
||||
echo
|
||||
local PARSED
|
||||
PARSED=$(getopt -o hdf:p:vq123456789 --long help,delete,format:,parallel:,verbose,quiet --name "taz" -- "$@")
|
||||
if [ $? -ne 0 ]; then
|
||||
disp E "Invalid options, use \"taz --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "taz: archive all files of a directory.\n\n"
|
||||
printf "Usage: taz [option] [--parallel=<n>] [--format=<format>] [directory1 ... directoryN]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\tDisplay that help screen\n"
|
||||
printf "\t-d, --delete\tDelete source file or directory after success\n"
|
||||
printf "\t-f, --format\tChose archive format in the given list. If several format are"
|
||||
printf "\t\t\tgiven, the smalest is kept\n"
|
||||
printf "\t-p, --parallel\tNumber of threads to use (if allowed by underlying utility)\n"
|
||||
printf "\t-v, --verbose\tDisplay progress where possible\n"
|
||||
printf "\t-q, --quiet\tDisplay less messages (only errors and warnings)\n"
|
||||
printf "\t-1, .., -9\tCompression level to use [1=fast/biggest, 9=slow/smallest]\n\n"
|
||||
printf "Supported archive format:\n"
|
||||
printf "\tParam.| programs | Algo. | Description\n"
|
||||
printf "\t------+---------------+-------+----------------------------------------\n"
|
||||
printf "\t lz | plzip, lzip | lzma | Safe efficient default format\n"
|
||||
printf "\t xz | xz | lzma2 | Unsafe, not for long term\n"
|
||||
printf "\t bz2 | pbzip2, bzip2 | bzip2 | Historical but less efficient than lz\n"
|
||||
printf "\t gz | pigz, gzip | lz77 | Historical, safe, fast\n"
|
||||
printf "\t lzo | lzop | lzo | Very fast but no multithread\n"
|
||||
printf "\t tar | tar | tar | No compression\n"
|
||||
printf "\n"
|
||||
return 0
|
||||
;;
|
||||
|
||||
"-d" | "--delete")
|
||||
-d|--delete)
|
||||
local willrm=1
|
||||
shift
|
||||
;;
|
||||
|
||||
"-f"?* | "--format"?*)
|
||||
local compform=$(echo "$opt" | cut -f 2- -d '=')
|
||||
-f|--format)
|
||||
local compform=$2
|
||||
shift 2
|
||||
;;
|
||||
|
||||
"-p"?* | "--parallel"?*)
|
||||
local nproc=$(echo "$opt" | cut -f 2- -d '=')
|
||||
-p|--parallel)
|
||||
local nproc=$2
|
||||
shift 2
|
||||
;;
|
||||
|
||||
"-v" | "--verbose")
|
||||
-v|--verbose)
|
||||
local verbose=1
|
||||
shift
|
||||
;;
|
||||
|
||||
"-q" | "--quiet")
|
||||
-q|--quiet)
|
||||
local quiet=1
|
||||
shift
|
||||
;;
|
||||
|
||||
"-"*)
|
||||
local complevel=$(echo $opt | sed 's/-//')
|
||||
if ! [[ $complevel =~ ^[1-9]+$ ]]; then
|
||||
disp E "Invalid option, use taz --help to display options list"
|
||||
echo
|
||||
return 1
|
||||
fi
|
||||
-[1-9])
|
||||
compression="${1#-}"
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
|
||||
*)
|
||||
local LIST="$LIST ${opt%/}"
|
||||
disp E "Invalid option, use \"taz --help\" to display options list"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# The remaining arguments after -- are the files/directories to process,
|
||||
# "." is used if none is given
|
||||
local FILES=("$@")
|
||||
[[ ${#FILES[@]} -eq 0 ]] && FILES=(".")
|
||||
|
||||
[[ ! $compform ]] && compform=lz # safe and efficient (unless data are already compressed)
|
||||
[[ ! $nproc ]] && nproc=1
|
||||
[[ ! $complevel ]] && complevel=6
|
||||
[[ $verbose -gt 1 && $quiet -gt 1 ]] &&
|
||||
disp E "The --verbose and --quiet options can't be used together."
|
||||
|
||||
for item in $LIST; do
|
||||
for item in "${FILES[@]}"; do
|
||||
local donetar=0
|
||||
disp I "Processing $item..."
|
||||
|
||||
@@ -537,6 +616,7 @@ taz()
|
||||
unset quiet
|
||||
}
|
||||
export -f taz
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -36,27 +36,22 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Display a backtrace
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: backtrace
|
||||
function backtrace()
|
||||
{
|
||||
echo "========= Call stack ========="
|
||||
typeset -i i=0
|
||||
|
||||
local func=
|
||||
for func in "${FUNCNAME[@]}"; do
|
||||
if [[ $i -ne 0 ]]; then
|
||||
printf "========= Call stack =========\n"
|
||||
local i=1 # We begin at 1 to ignore backtrace itself
|
||||
while [[ $i -lt ${#FUNCNAME[@]} ]]; do
|
||||
printf '%15s() %s:%d\n' \
|
||||
"$func" "${BASH_SOURCE[$i]}" "${BASH_LINENO[(($i - 1))]}"
|
||||
fi
|
||||
let i++ || true
|
||||
"${FUNCNAME[$i]}" "${BASH_SOURCE[$i]}" "${BASH_LINENO[$(( i-1 ))]}"
|
||||
((i++))
|
||||
done
|
||||
unset func i
|
||||
echo "=============================="
|
||||
unset i
|
||||
printf "==============================\n"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Function to be trapped for errors investigation
|
||||
# ------------------------------------------------------------------------------
|
||||
function error()
|
||||
{
|
||||
local errcode=$?
|
||||
@@ -66,45 +61,70 @@ function error()
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Activate or deactivate error trapping to display backtrace
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: settrace <--on|--off|--status>
|
||||
settrace()
|
||||
{
|
||||
local status="off"
|
||||
[[ $(trap -p ERR) ]] && status="on"
|
||||
#trap -p ERR
|
||||
for opt in $@; do
|
||||
case $opt in
|
||||
"-h" | "--help")
|
||||
echo "Try to activate backtrace display for script debugging."
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " --on Activate backtrace generation"
|
||||
echo " --off Deactivate backtrace generation"
|
||||
echo
|
||||
echo "That function active a trap event on error. If the script you want to"
|
||||
echo "debug overload the ERR bash trap, it will not work."
|
||||
echo
|
||||
|
||||
local PARSED
|
||||
PARSED=$(getopt -oh --long help,on,off,status,force -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"settrace --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
local force=0
|
||||
while true; do
|
||||
case $1 in
|
||||
-h|--help)
|
||||
printf "Try to activate backtrace display for script debugging.\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t--on\t\tActivate backtrace generation\n"
|
||||
printf "\t--force\t\tForce replacement of existing trap (use with --on)\n"
|
||||
printf "\t--off\t\tDeactivate backtrace generation\n\n"
|
||||
printf "That function active a trap event on error. If the script you want to\n"
|
||||
printf "debug overload the ERR bash trap, it will not work.\n"
|
||||
return 0
|
||||
;;
|
||||
"--on")
|
||||
if [[ $status == "on" ]]; then
|
||||
disp W "ERR signal trap is already set, replacing previous trap!"
|
||||
--on)
|
||||
if [[ ${status} == "on" ]] && [[ $force -eq 0 ]]; then
|
||||
disp E "ERR signal trap is already set. Use --force to replace it."
|
||||
return 1
|
||||
fi
|
||||
trap "error" ERR
|
||||
shift
|
||||
;;
|
||||
"--off")
|
||||
if [[ $status != "on" ]]; then
|
||||
--force)
|
||||
force=1
|
||||
shift
|
||||
;;
|
||||
--off)
|
||||
if [[ ${status} != "on" ]]; then
|
||||
disp W "ERR signal trap is already unset!"
|
||||
fi
|
||||
trap - ERR
|
||||
shift
|
||||
;;
|
||||
"--status")
|
||||
disp "ERR trap signal is ${status}."
|
||||
--status)
|
||||
disp I "Trap signal is ${status}."
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid options, use \"settrace --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
unset status
|
||||
unset status force
|
||||
}
|
||||
export -f settrace
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -36,11 +36,10 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Color definitions
|
||||
# ------------------------------------------------------------------------------
|
||||
# Standard 16 colors display declaration
|
||||
export DEFAULTFG="\e[0;39m"
|
||||
export DEFAULTBG="\e[0;49m"
|
||||
export DEFAULTCOL=${DEFAULTBG}${DEFAULTFG}
|
||||
export DEFAULTFG='\e[0;39m'
|
||||
export DEFAULTBG='\e[0;49m'
|
||||
export DEFAULTCOL="${DEFAULTBG}${DEFAULTFG}"
|
||||
export RESETCOL=$'\e[0m'
|
||||
|
||||
# Regular Colors
|
||||
@@ -112,31 +111,58 @@ export On_IBlue='\e[0;104m'
|
||||
export On_IPurple='\e[0;105m'
|
||||
export On_ICyan='\e[0;106m'
|
||||
export On_IWhite='\e[0;107m'
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Display a message
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: disp <type> <message>
|
||||
# Types:
|
||||
# I : info (green)
|
||||
# W : warning (yellow)
|
||||
# E : error (red)
|
||||
# D : debug (cyan)
|
||||
disp()
|
||||
{
|
||||
case $1 in
|
||||
# Handle NO_COLOR: disable colors if set
|
||||
local color_enabled=1
|
||||
[[ -n $NO_COLOR ]] && color_enabled=0
|
||||
|
||||
case ${1^^} in
|
||||
"I")
|
||||
if [[ $color_enabled -eq 1 ]]; then
|
||||
local heads="[ ${IGreen}info${DEFAULTFG} ]"
|
||||
else
|
||||
local heads="[ info ]"
|
||||
fi
|
||||
shift
|
||||
[[ -z $QUIET || $QUIET -ne 1 ]] && \
|
||||
printf "%b\n" "${heads} $*${RESETCOL}"
|
||||
;;
|
||||
"W")
|
||||
if [[ $color_enabled -eq 1 ]]; then
|
||||
local heads="[ ${IYellow}Warning${DEFAULTFG} ]"
|
||||
else
|
||||
local heads="[ Warning ]"
|
||||
fi
|
||||
shift
|
||||
printf "%b\n" "${heads} $*${RESETCOL}" >&2
|
||||
;;
|
||||
"E")
|
||||
if [[ $color_enabled -eq 1 ]]; then
|
||||
local heads="[ ${IRed}ERROR${DEFAULTFG} ]"
|
||||
else
|
||||
local heads="[ ERROR ]"
|
||||
fi
|
||||
shift
|
||||
printf "%b\n" "${heads} $*${RESETCOL}" >&2
|
||||
;;
|
||||
"D")
|
||||
if [[ $color_enabled -eq 1 ]]; then
|
||||
local heads="[ ${ICyan}debug${DEFAULTFG} ]"
|
||||
else
|
||||
local heads="[ debug ]"
|
||||
fi
|
||||
shift
|
||||
[[ -n $DEBUG && $DEBUG -gt 1 ]] && \
|
||||
printf "%b\n" "${heads} $*${RESETCOL}"
|
||||
@@ -149,4 +175,9 @@ disp()
|
||||
}
|
||||
export -f disp
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# Load disp section variables
|
||||
load_conf disp
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -35,223 +35,426 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# expandlist : treat wildcards in a file/directory list
|
||||
# ------------------------------------------------------------------------------
|
||||
# Expand wildcards in a file/directory list and quote the results
|
||||
# Usage: expandlist [options] <item1 [item2 ... itemN]>
|
||||
expandlist()
|
||||
{
|
||||
local result=""
|
||||
for item in "$1"; do
|
||||
for content in "$item"; do
|
||||
result+="\"$content\" "
|
||||
done
|
||||
done
|
||||
echo $result
|
||||
}
|
||||
local separator=" "
|
||||
local PARSED
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Clean a directory or a tree from temporary or backup files
|
||||
# ------------------------------------------------------------------------------
|
||||
clean()
|
||||
{
|
||||
for opt in $@; do
|
||||
case $opt in
|
||||
"-r" | "--recurs")
|
||||
local recursive=1
|
||||
;;
|
||||
PARSED=$(getopt -o hs:n --long help,separator:,newline -n 'expandlist' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"expandlist --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
"-h" | "--help")
|
||||
echo "clean: erase backup files in the given directories."
|
||||
echo
|
||||
echo "Usage: clean [option] [directory1] [...[directoryX]]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -h, --help Display that help screen"
|
||||
echo " -r, --recurs Do a recursive cleaning"
|
||||
echo " -f, --force Do not ask for confirmation (use with care)"
|
||||
echo " -s, --shell Do nothing and display what will be executed"
|
||||
echo
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "expandlist: expand globs and wrap matched items in double quotes.\n\n"
|
||||
printf "Usage: expandlist [options] <item1 [item2 ... itemN]>\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
printf "\t-s, --separator SEP\tSet output separator (default: space)\n"
|
||||
printf "\t-n, --newline\t\tUse a newline as separator\n"
|
||||
return 0
|
||||
;;
|
||||
|
||||
"-s" | "--shell")
|
||||
local outshell=1
|
||||
-s|--separator)
|
||||
separator="$2"
|
||||
shift 2
|
||||
;;
|
||||
|
||||
"-f" | "--force")
|
||||
local force=1
|
||||
-n|--newline)
|
||||
separator=$'\n'
|
||||
shift
|
||||
;;
|
||||
|
||||
"-"*)
|
||||
disp E "Invalid option, use \"clean --help\" to display usage."
|
||||
echo
|
||||
return 1
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
|
||||
*)
|
||||
local dirlist="$dirlist $opt"
|
||||
disp E "Invalid options, use \"expandlist --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[ ! $dirlist ]] && local dirlist=$(pwd)
|
||||
local item="" result="" matched=0
|
||||
shopt -s nullglob
|
||||
|
||||
[[ ! $recursive ]] && local findopt="-maxdepth 1"
|
||||
[[ ! $force ]] && local rmopt="-i"
|
||||
unset recursive force
|
||||
for item in "$@"; do
|
||||
local expanded=()
|
||||
|
||||
for dir in $dirlist; do
|
||||
local dellist=$(find "$dir" $findopt -type f -name "*~" -o -name "#*#" \
|
||||
-o -name "*.bak" -o -name ".~*#")
|
||||
for f in $dellist; do
|
||||
if [[ ! $outshell ]]; then
|
||||
rm $rmopt $f
|
||||
# True glob expansion when wildcards are present.
|
||||
if [[ "$item" == *'*'* || "$item" == *'?'* || "$item" == *'['* ]]; then
|
||||
expanded=( $item )
|
||||
else
|
||||
echo "rm $rmopt $f"
|
||||
expanded=( "$item" )
|
||||
fi
|
||||
|
||||
if [[ ${#expanded[@]} -eq 0 ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
for content in "${expanded[@]}"; do
|
||||
if (( matched )); then
|
||||
result+="$separator"
|
||||
fi
|
||||
result+="\"$content\""
|
||||
matched=1
|
||||
done
|
||||
done
|
||||
|
||||
shopt -u nullglob
|
||||
printf '%s\n' "$result"
|
||||
}
|
||||
export -f expandlist
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Clean a directory tree from temporary or backup files
|
||||
# Usage: clean [options] [directory1] [...[directoryX]]
|
||||
# Options:
|
||||
# -h, --help: display help screen
|
||||
# -r, --recurs: do a recursive cleaning
|
||||
# -f, --force: do not ask for confirmation (use with care)
|
||||
# -s, --shell: do nothing and display what will be executed
|
||||
clean()
|
||||
{
|
||||
local recursive=0 force=0 outshell=0
|
||||
|
||||
# Define short and long options
|
||||
local PARSED
|
||||
PARSED=$(getopt -o hrsf --long help,recurs,shell,force -n 'clean' -- "$@")
|
||||
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"clean --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-r|--recurs)
|
||||
recursive=1
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
printf "clean: erase backup files in the given directories.\n\n"
|
||||
printf "Usage: clean [option] [directory1] [...[directoryX]]\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay that help screen\n"
|
||||
printf "\t-r, --recurs\t\tDo a recursive cleaning\n"
|
||||
printf "\t-f, --force\t\tDo not ask for confirmation (use with care)\n"
|
||||
printf "\t-s, --shell\t\tDo nothing and display what will be executed\n"
|
||||
printf "\n"
|
||||
return 0
|
||||
;;
|
||||
-s|--shell)
|
||||
outshell=1
|
||||
shift
|
||||
;;
|
||||
-f|--force)
|
||||
force=1
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid parameter, use \"clean --help\" to display options list"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Handle remaining arguments as directories
|
||||
local dirlist=("$@")
|
||||
[[ ${#dirlist[@]} -eq 0 ]] && dirlist=(".")
|
||||
|
||||
local findopt=() rmopt=()
|
||||
(( ! recursive )) && findopt=(-maxdepth 1)
|
||||
(( ! force )) && rmopt=(-i)
|
||||
|
||||
for dir in "${dirlist[@]}"; do
|
||||
find "$dir" "${findopt[@]}" -type f \( -name "*~" -o -name "#*#" -o -name "*.bak" -o -name ".~*#" \) -print0 |
|
||||
while IFS= read -r -d '' f; do
|
||||
if (( outshell )); then
|
||||
if (( ${#rmopt[@]} )); then
|
||||
printf 'rm %s -- "%s"\n' "${rmopt[*]}" "$f"
|
||||
else
|
||||
printf 'rm -- "%s"\n' "$f"
|
||||
fi
|
||||
else
|
||||
rm "${rmopt[@]}" -- "$f"
|
||||
fi
|
||||
done
|
||||
done
|
||||
unset outshell dirlist dellist findopt rmopt
|
||||
}
|
||||
export -f clean
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Create a directory then goes inside
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: mcd <directory>
|
||||
mcd()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
printf "mcd: Create a directory and enter it.\n\n"
|
||||
printf "Usage: mcd <directory>\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ ! $# -eq 1 ]]; then
|
||||
disp E "Create a directory then goes inside."
|
||||
disp E "Usage: mcd <directory>"
|
||||
disp E "Missing parameter. Use \"mcd --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
mkdir -pv "$1" && cd "$1" || echo "Failed create or change directory."
|
||||
mkdir -pv "$1" && cd "$1" || {
|
||||
printf "Failed create and/or change directory.\n"
|
||||
return 1
|
||||
}
|
||||
}
|
||||
export -f mcd
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Rename all files in current directory to replace spaces with _
|
||||
# ------------------------------------------------------------------------------
|
||||
# Rename files and directories to replace spaces with another character
|
||||
# Usage: rmspc [options]
|
||||
# Options:
|
||||
# -h, --help: display help screen
|
||||
# -r, --recursive: treat subdirectories of the given directory
|
||||
# -c, --subst-char: change the replacement character (default is underscore)
|
||||
# -v, --verbose: display more details (recursive mode only)
|
||||
# -s, --shell: do nothing and display commands that would be executed
|
||||
rmspc()
|
||||
{
|
||||
local lst=""
|
||||
for opt in $@; do
|
||||
case $opt in
|
||||
"-h" | "--help")
|
||||
echo "rmspc: remove spaces from all filenames in current directories"
|
||||
echo
|
||||
echo "Usage: rmspc [option]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -h, --help Display that help screen"
|
||||
echo " -r, --recursive Treat subdirectories of the given directory"
|
||||
echo " -c, --subst-char Change the replacement character (default is underscore)"
|
||||
echo " -v, --verbose Display more details (recursive mode only)"
|
||||
echo " -s, --shell Do nothing and display commands that would be executed"
|
||||
echo
|
||||
echo "Note: if the --subst-char option is given without parameters, spaces will be"
|
||||
echo " replaced with nothing (concatenation)."
|
||||
echo
|
||||
local recurs=0 verb=0 shell=0
|
||||
local substchar="_" substchar_set=0
|
||||
local mvopt=()
|
||||
local PARSED
|
||||
|
||||
PARSED=$(getopt -o hr:c::vs --long help,recursive,subst-char::,verbose,shell -n 'rmspc' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"rmspc --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "rmspc: remove spaces from all filenames in current directories\n\n"
|
||||
printf "Usage: rmspc [option]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay that help screen\n"
|
||||
printf "\t-r, --recursive\t\tTreat subdirectories of the given directory\n"
|
||||
printf "\t-c, --subst-char\tChange the replacement character (default is underscore)\n"
|
||||
printf "\t-v, --verbose\t\tDisplay more details (recursive mode only)\n"
|
||||
printf "\t-s, --shell\t\tDo nothing and display commands that would be executed\n\n"
|
||||
printf "Note: if the --subst-char option is given without parameters, spaces will be\n"
|
||||
printf " replaced with nothing (concatenation).\n"
|
||||
return 0
|
||||
;;
|
||||
|
||||
"-r" | "--recursive")
|
||||
local recurs=1
|
||||
-r|--recursive)
|
||||
recurs=1
|
||||
shift
|
||||
;;
|
||||
|
||||
"-c"?* | "--subst-char"?*)
|
||||
if [[ $(echo $opt | grep "=") ]]; then
|
||||
local substchar=$(echo "$opt" | cut -f 2- -d '=')
|
||||
else
|
||||
local substchar='none'
|
||||
fi
|
||||
-c|--subst-char)
|
||||
substchar_set=1
|
||||
substchar="$2"
|
||||
shift 2
|
||||
;;
|
||||
|
||||
"-v" | "--verbose")
|
||||
local verb=1
|
||||
-v|--verbose)
|
||||
verb=1
|
||||
shift
|
||||
;;
|
||||
|
||||
"-s" | "--shell")
|
||||
local shell=1
|
||||
-s|--shell)
|
||||
shell=1
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
|
||||
*)
|
||||
disp E "Invalid parameter, use \"rmspc --help\" to display options list"
|
||||
echo
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[ ! $substchar ]] && substchar="_"
|
||||
[[ $substchar == "none" ]] && local substchar=""
|
||||
[[ $verb ]] && local mvopt="-v"
|
||||
[[ "$substchar" == "none" ]] && substchar=""
|
||||
(( verb )) && mvopt=(-v)
|
||||
|
||||
shopt -s nullglob
|
||||
for f in *; do
|
||||
[[ $recurs ]] && [[ -d "$f" ]] && (
|
||||
[[ $verb ]] && disp I "Entering directory $(pwd)/$f ..."
|
||||
if (( recurs )) && [[ -d "$f" ]]; then
|
||||
(
|
||||
local lastdir=$f
|
||||
pushd "$f" >/dev/null
|
||||
rmspc $@
|
||||
popd >/dev/null
|
||||
[[ $verb ]] && disp I "Leaving directory $(pwd)/$lastdir"
|
||||
unset lastdir
|
||||
)
|
||||
(( verb )) && disp I "Entering directory $(pwd)/$f ..."
|
||||
pushd "$f" >/dev/null || return 1
|
||||
|
||||
if [[ $(echo $f | grep " ") ]]; then
|
||||
local newf="${f// /${substchar}}"
|
||||
local command="mv $mvopt \"$f\" \"$newf\""
|
||||
if [[ $shell ]]; then
|
||||
echo $command
|
||||
if (( substchar_set )); then
|
||||
rmspc ${recurs:+-r} -c "$substchar" ${verb:+-v} ${shell:+-s}
|
||||
else
|
||||
$command
|
||||
rmspc ${recurs:+-r} ${verb:+-v} ${shell:+-s}
|
||||
fi
|
||||
|
||||
popd >/dev/null || return 1
|
||||
(( verb )) && disp I "Leaving directory $(pwd)/$lastdir"
|
||||
)
|
||||
fi
|
||||
|
||||
if [[ "$f" == *" "* ]]; then
|
||||
local newf="${f// /${substchar}}"
|
||||
[[ "$f" == "$newf" ]] && continue
|
||||
if (( shell )); then
|
||||
if (( ${#mvopt[@]} )); then
|
||||
printf 'mv %s -- "%s" "%s"\n' "${mvopt[*]}" "$f" "$newf"
|
||||
else
|
||||
printf 'mv -- "%s" "%s"\n' "$f" "$newf"
|
||||
fi
|
||||
else
|
||||
mv "${mvopt[@]}" -- "$f" "$newf" || {
|
||||
disp E "Failed renaming \"$f\" to \"$newf\"."
|
||||
continue
|
||||
}
|
||||
fi
|
||||
fi
|
||||
done
|
||||
unset lst substchar verb shell newf command mvopt
|
||||
shopt -u nullglob
|
||||
}
|
||||
export -f rmspc
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# display stats about a file structure
|
||||
# ------------------------------------------------------------------------------
|
||||
# Display statistics about a file tree
|
||||
# Usage: file_stats [options] [path]
|
||||
# Options:
|
||||
# -H, --human Human readable sizes\n"
|
||||
# -d, --details Display details (min/max/average/median)
|
||||
# -m, --average Display only average size
|
||||
# -M, --median Display only median size
|
||||
# -c, --count Display only count of files
|
||||
# -t, --total Display only total size
|
||||
# -a, --all Display all stats in human readable format (shortcut for -H -d)
|
||||
# -x, --ext [ext] Filter by extension (e.g. -x log for .log files)
|
||||
# -X, --ext-list [list] Filter by multiple extensions (e.g. -X log,txt)
|
||||
# --min [size] Minimum size (e.g., 10M)
|
||||
# --max [size] Maximum size (e.g., 100M)
|
||||
file_stats()
|
||||
{
|
||||
local human=0 details=0 only_avg=0 only_med=0 only_count=0 only_total=0
|
||||
local path="." show_all=1 ext_filter="" ext_list="" min_size="" max_size=""
|
||||
local OPTIND opt
|
||||
|
||||
# Analyse options
|
||||
while [[ "$1" =~ ^- ]]; do
|
||||
local PARSED
|
||||
# Short: H, d, m, M, c, t, a, x:, X:
|
||||
# Long: human, details, average, median, count, total, all, ext:, ext-list:, min:, max:, help
|
||||
PARSED=$(getopt -o HdmMctax:X:h --long human,details,average,median,count,total,all,ext:,ext-list:,min:,max:,help -n 'file_stats' -- "$@")
|
||||
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"file_stats --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-H) human=1 ;;
|
||||
-d) details=1 ;;
|
||||
-m) only_avg=1; show_all=0 ;;
|
||||
-M) only_med=1; show_all=0 ;;
|
||||
-c) only_count=1; show_all=0 ;;
|
||||
-t) only_total=1; show_all=0 ;;
|
||||
-a) human=1; details=1 ;;
|
||||
-x) ext_filter="${2#.}"; shift ;;
|
||||
-X) ext_list="${2}"; shift ;;
|
||||
--min) min_size="$2"; shift ;;
|
||||
--max) max_size="$2"; shift ;;
|
||||
--) shift; break ;;
|
||||
-*) echo "Usage: file_stats [-h] [-d] [-mMctaxX --min N --max N] [path]"; return 1 ;;
|
||||
-h|--help)
|
||||
printf "Usage: file_stats [options] [path]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-H, --human\t\tHuman readable sizes\n"
|
||||
printf "\t-d, --details\t\tShow detailed histogram\n"
|
||||
printf "\t-m, --average\t\tShow only average size\n"
|
||||
printf "\t-M, --median\t\tShow only median size\n"
|
||||
printf "\t-c, --count\t\tShow only file count\n"
|
||||
printf "\t-t, --total\t\tShow only total size\n"
|
||||
printf "\t-a, --all\t\tShow all (human + details)\n"
|
||||
printf "\t-x, --ext [ext]\t\tFilter by extension\n"
|
||||
printf "\t-X, --ext-list [list]\tFilter by comma-separated list\n"
|
||||
printf "\t--min [size]\t\tMinimum size (e.g., 10M)\n"
|
||||
printf "\t--max [size]\t\tMaximum size (e.g., 100M)\n"
|
||||
return 0 ;;
|
||||
-H|--human)
|
||||
human=1
|
||||
shift
|
||||
;;
|
||||
-d|--details)
|
||||
details=1
|
||||
shift
|
||||
;;
|
||||
-m|--average)
|
||||
only_avg=1
|
||||
show_all=0
|
||||
shift
|
||||
;;
|
||||
-M|--median)
|
||||
only_med=1
|
||||
show_all=0
|
||||
shift
|
||||
;;
|
||||
-c|--count)
|
||||
only_count=1
|
||||
show_all=0
|
||||
shift
|
||||
;;
|
||||
-t|--total)
|
||||
only_total=1
|
||||
show_all=0
|
||||
shift
|
||||
;;
|
||||
-a|--all)
|
||||
human=1
|
||||
details=1
|
||||
shift
|
||||
;;
|
||||
-x|--ext)
|
||||
ext_filter="${2#.}"
|
||||
shift 2
|
||||
;;
|
||||
-X|--ext-list)
|
||||
ext_list="$2"
|
||||
shift 2
|
||||
;;
|
||||
--min)
|
||||
min_size="$2"
|
||||
shift 2
|
||||
;;
|
||||
--max)
|
||||
max_size="$2"
|
||||
shift 2
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid option: $1"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
[ -n "$1" ] && path="$1"
|
||||
[[ -n "$1" ]] && path="$1"
|
||||
|
||||
# Prepare find filters
|
||||
local find_cmd=(find "$path" -type f)
|
||||
|
||||
# Extension simple
|
||||
# Single extension filter
|
||||
if [[ -n "$ext_filter" ]]; then
|
||||
find_cmd+=(-iname "*.$ext_filter")
|
||||
fi
|
||||
|
||||
# Extension liste
|
||||
# Extension list filter
|
||||
if [[ -n "$ext_list" ]]; then
|
||||
IFS=',' read -ra exts <<< "$ext_list"
|
||||
find_cmd+=('(')
|
||||
@@ -262,7 +465,7 @@ file_stats()
|
||||
find_cmd+=(')')
|
||||
fi
|
||||
|
||||
# Taille min/max (à évaluer en octets)
|
||||
# Minimum/maximum size filters (evaluated in bytes)
|
||||
if [[ -n "$min_size" ]]; then
|
||||
find_cmd+=(-size +"$(numfmt --from=iec "$min_size")"c)
|
||||
fi
|
||||
@@ -270,13 +473,28 @@ file_stats()
|
||||
find_cmd+=(-size -"$(( $(numfmt --from=iec "$max_size") + 1 ))"c)
|
||||
fi
|
||||
|
||||
# Exécution
|
||||
"${find_cmd[@]}" -printf "%s\n" 2>/dev/null | sort -n | awk -v human="$human" -v details="$details" -v only_avg="$only_avg" -v only_med="$only_med" -v only_count="$only_count" -v only_total="$only_total" -v show_all="$show_all" -v path="$path" '
|
||||
# Execution
|
||||
"${find_cmd[@]}" -printf "%s\n" 2>/dev/null | sort -n | \
|
||||
awk -v human="$human" -v details="$details" -v only_avg="$only_avg" \
|
||||
-v only_med="$only_med" -v only_count="$only_count" \
|
||||
-v only_total="$only_total" -v show_all="$show_all" -v path="$path" '
|
||||
# Convert function
|
||||
function human_readable(x) {
|
||||
split("B KiB MiB GiB TiB", units)
|
||||
for (i=1; x>=1024 && i<5; i++) x /= 1024
|
||||
i = 1
|
||||
while (x >= 1024 && i < 5) {
|
||||
x /= 1024
|
||||
i++
|
||||
}
|
||||
return sprintf("%.2f %s", x, units[i])
|
||||
}
|
||||
|
||||
# Display function
|
||||
function out(label, val, is_size) {
|
||||
if (human == 1 && is_size == 1) val = human_readable(val)
|
||||
printf "%-20s : %s\n", label, val
|
||||
}
|
||||
|
||||
{
|
||||
sizes[NR] = $1
|
||||
total += $1
|
||||
@@ -288,50 +506,56 @@ file_stats()
|
||||
bucket[b]++
|
||||
}
|
||||
}
|
||||
|
||||
END {
|
||||
count = NR
|
||||
if (count == 0) {
|
||||
print "Aucun fichier trouvé."; exit
|
||||
print "No files found."
|
||||
exit
|
||||
}
|
||||
|
||||
moyenne = total / count
|
||||
if (count % 2 == 1)
|
||||
mediane = sizes[(count + 1) / 2]
|
||||
else
|
||||
mediane = (sizes[count / 2] + sizes[count / 2 + 1]) / 2
|
||||
average = total / count
|
||||
|
||||
function out(label, val) {
|
||||
if (human) val = human_readable(val)
|
||||
printf "%-20s : %s\n", label, val
|
||||
# Median calculation: exact using sorted array values
|
||||
if (count % 2 == 1) {
|
||||
median = sizes[(count + 1) / 2]
|
||||
} else {
|
||||
idx = count / 2
|
||||
median = (sizes[idx] + sizes[idx + 1]) / 2
|
||||
}
|
||||
|
||||
if (only_avg) out("Taille moyenne", moyenne)
|
||||
else if (only_med) out("Taille médiane", mediane)
|
||||
else if (only_count) printf "Nombre de fichiers : %d\n", count
|
||||
else if (only_total) out("Taille totale", total)
|
||||
if (only_avg) out("Average size", average, 1)
|
||||
else if (only_med) out("Median size", median, 1)
|
||||
else if (only_count) out("Number of files", count, 0)
|
||||
else if (only_total) out("Total size", total, 1)
|
||||
else {
|
||||
if (show_all || human || details) {
|
||||
printf "Statistiques sur \"%s\"\n", path
|
||||
printf "Statistics for \"%s\"\n", path
|
||||
printf "-------------------------\n"
|
||||
}
|
||||
out("Nombre de fichiers", count)
|
||||
out("Taille totale", total)
|
||||
out("Taille moyenne", moyenne)
|
||||
out("Taille médiane", mediane)
|
||||
out("Taille minimale", min)
|
||||
out("Taille maximale", max)
|
||||
out("Number of files", count, 0)
|
||||
out("Total size", total, 1)
|
||||
out("Average size", average, 1)
|
||||
out("Median size", median, 1)
|
||||
out("Minimum size", min, 1)
|
||||
out("Maximum size", max, 1)
|
||||
}
|
||||
|
||||
if (details) {
|
||||
print "\nHistogramme des tailles :"
|
||||
for (i = 0; i in bucket; i++) {
|
||||
low = 2^i
|
||||
high = 2^(i+1)
|
||||
if (i == 0)
|
||||
label = sprintf("%4s – %4s", "0", "1K")
|
||||
else
|
||||
label = sprintf("%4s – %4s", human_readable(low), human_readable(high))
|
||||
printf "%-20s : %5d fichiers\n", label, bucket[i]
|
||||
print "\nSize histogram:"
|
||||
|
||||
# Use a separate array for the loop to avoid collision
|
||||
for (b in bucket) {
|
||||
# Pre-calculate label parts
|
||||
# 1024^0 = 1 (B), 1024^1 = 1K, etc.
|
||||
low = (b == 0) ? 0 : (1024^b)
|
||||
high = 1024^(b+1)
|
||||
|
||||
label = sprintf("%-9s – %-9s",
|
||||
(b == 0) ? "0" : human_readable(low),
|
||||
human_readable(high))
|
||||
|
||||
# We store buckets in an array, access them by index b
|
||||
printf "%-25s : %6d fichiers\n", label, bucket[b]
|
||||
}
|
||||
}
|
||||
}'
|
||||
@@ -341,4 +565,231 @@ export -f file_stats
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Find the biggest files in a directory tree
|
||||
# Usage: findbig [options] [directory]
|
||||
# Options:
|
||||
# -h : display help screen
|
||||
# -d : display details (ls -l) for each file
|
||||
# -x : do not cross filesystem boundaries
|
||||
# -l : limit : number of files to return (default is 10)
|
||||
findbig()
|
||||
{
|
||||
local details=0 limit=10 one_fs=0
|
||||
|
||||
local PARSED
|
||||
PARSED=$(getopt -o hdl:x --long help,details,limit:,one-fs -n 'findbig' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"findbig --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "findbig: Find the N biggest files in a directory tree.\n\n"
|
||||
printf "Usage: findbig [options] [directory]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
printf "\t-d, --details\t\tShow detailed file info (ls -ld)\n"
|
||||
printf "\t-l, --limit N\t\tNumber of files to return (default: 10)\n"
|
||||
printf "\t-x, --one-fs\t\tDo not cross filesystem boundaries\n"
|
||||
return 0
|
||||
;;
|
||||
-d|--details)
|
||||
details=1
|
||||
shift
|
||||
;;
|
||||
-l|--limit)
|
||||
limit="$2"
|
||||
[[ "$limit" =~ ^[0-9]+$ ]] || {
|
||||
disp E "Invalid limit: must be a positive integer."
|
||||
return 1
|
||||
}
|
||||
shift 2
|
||||
;;
|
||||
-x|--one-fs)
|
||||
one_fs=1
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid option: $1"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
local dir="${1:-.}"
|
||||
|
||||
# Prepare find arguments in an array for cleaner handling
|
||||
local find_args=(-L "$dir")
|
||||
(( one_fs )) && find_args+=(-xdev)
|
||||
find_args+=(-type f)
|
||||
|
||||
# Logic: find files, print size and path, sort numeric reverse, take N
|
||||
if (( details )); then
|
||||
find "${find_args[@]}" -printf "%s %p\n" 2>/dev/null | sort -rn | head -n "$limit" |
|
||||
while IFS= read -r line; do
|
||||
local path="${line#* }"
|
||||
ls -ld -- "$path"
|
||||
done
|
||||
else
|
||||
find "${find_args[@]}" -printf "%s %p\n" 2>/dev/null | sort -rn | head -n "$limit"
|
||||
fi
|
||||
}
|
||||
export -f findbig
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Find empty files in a directory tree
|
||||
# Usage: findzero [options] [directory]
|
||||
# Options:
|
||||
# -h : display help screen
|
||||
# -d : display details (ls -l) for each file
|
||||
# -x : do not cross filesystem boundaries
|
||||
# --delete : delete empty files and display their paths
|
||||
findzero()
|
||||
{
|
||||
local delete=0 details=0 one_fs=0
|
||||
|
||||
local PARSED
|
||||
# o: options, long: long equivalents
|
||||
PARSED=$(getopt -o hdx --long help,details,one-fs,delete -n 'findzero' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"findzero --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "findzero: Find or delete empty files in a directory tree.\n\n"
|
||||
printf "Usage: findzero [options] [directory]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
printf "\t-d, --details\t\tShow detailed file info (ls -ls)\n"
|
||||
printf "\t-x, --one-fs\t\tDo not cross filesystem boundaries\n"
|
||||
printf "\t--delete\t\tActually remove the empty files\n"
|
||||
return 0 ;;
|
||||
-d|--details)
|
||||
details=1
|
||||
shift
|
||||
;;
|
||||
-x|--one-fs)
|
||||
one_fs=1
|
||||
shift
|
||||
;;
|
||||
--delete)
|
||||
delete=1
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid option: $1"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
local dir="${1:-.}"
|
||||
local find_args=("-L" "$dir" "-type" "f" "-empty")
|
||||
|
||||
(( one_fs )) && find_args+=("-xdev")
|
||||
|
||||
# Execution logic
|
||||
if (( delete )); then
|
||||
disp W "Deleting empty files in $dir..."
|
||||
find "${find_args[@]}" -delete -print
|
||||
elif (( details )); then
|
||||
find "${find_args[@]}" -ls
|
||||
else
|
||||
find "${find_args[@]}"
|
||||
fi
|
||||
}
|
||||
export -f findzero
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Find dead symbolic links in a directory tree
|
||||
# Usage: finddead [options] [directory]
|
||||
# Options:
|
||||
# -h : display help screen
|
||||
# -d : display details (ls -l) for each link
|
||||
# -x : do not cross filesystem boundaries
|
||||
# --delete : delete dead links and display their paths
|
||||
finddead()
|
||||
{
|
||||
local delete=0 details=0 one_fs=0
|
||||
|
||||
local PARSED
|
||||
PARSED=$(getopt -o hdx --long help,details,one-fs,delete -n 'finddead' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"finddead --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "finddead: Find or delete dead/broken symbolic links.\n\n"
|
||||
printf "Usage: finddead [options] [directory]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
printf "\t-d, --details\t\tShow detailed symlink info (ls -ls)\n"
|
||||
printf "\t-x, --one-fs\t\tDo not cross filesystem boundaries\n"
|
||||
printf "\t--delete\t\tActually remove the dead links\n"
|
||||
return 0 ;;
|
||||
-d|--details)
|
||||
details=1
|
||||
shift
|
||||
;;
|
||||
-x|--one-fs)
|
||||
one_fs=1
|
||||
shift
|
||||
;;
|
||||
--delete)
|
||||
delete=1
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid option: $1"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
local dir="${1:-.}"
|
||||
# -xtype l searches for links that do not point to an existing file
|
||||
local find_args=("$dir" "-xtype" "l")
|
||||
(( one_fs )) && find_args+=("-xdev")
|
||||
|
||||
# Execution logic
|
||||
if (( delete )); then
|
||||
disp W "Deleting dead symlinks in $dir..."
|
||||
find "${find_args[@]}" -delete -print
|
||||
elif (( details )); then
|
||||
find "${find_args[@]}" -ls
|
||||
else
|
||||
find "${find_args[@]}"
|
||||
fi
|
||||
}
|
||||
export -f finddead
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -36,35 +36,77 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Make non-IT peoples think you're busy doing something hard
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: busy [options] [pattern]
|
||||
# Options:
|
||||
# --delay=<ms> : add a delay between each line output (milliseconds)
|
||||
# pattern : the string to search for in the hexdump output (default is "ca fe")
|
||||
busy()
|
||||
{
|
||||
local pattern="ca fe"
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--delay=*)
|
||||
delay_ms="${arg#*=}"
|
||||
if ! [[ $delay_ms =~ ^[0-9]+$ ]]; then
|
||||
disp E "Invalid delay value, must be an integer (milliseconds)."
|
||||
local pattern="ca fe" delay_ms=0
|
||||
|
||||
local PARSED
|
||||
# Short: h, p:, d:
|
||||
# Long: help, pattern:, delay:
|
||||
PARSED=$(getopt -o hp:d: --long help,pattern:,delay: -n 'busy' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"busy --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "busy: Monitor /dev/urandom for a specific pattern.\n\n"
|
||||
printf "Usage: busy [options] [pattern]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
printf "\t-p, --pattern PATTERN\tHex pattern to search (default: \"ca fe\")\n"
|
||||
printf "\t-d, --delay MS\t\tDelay between matches in milliseconds\n"
|
||||
return 0
|
||||
;;
|
||||
-p|--pattern)
|
||||
pattern="$2"
|
||||
shift 2
|
||||
;;
|
||||
-d|--delay)
|
||||
delay_ms="$2"
|
||||
if ! [[ "$delay_ms" =~ ^[0-9]+$ ]]; then
|
||||
disp E "Invalid delay: must be an integer (milliseconds)."
|
||||
return 1
|
||||
fi
|
||||
shift 2
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
pattern="$arg"
|
||||
disp E "Invalid option: $1"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Convert milliseconds to seconds for 'sleep'
|
||||
local delay_s=$(awk "BEGIN { printf \"%.3f\", $delay_ms / 1000 }")
|
||||
local delay_s=$(awk "BEGIN{
|
||||
printf \"%.3f\", $delay_ms / 1000 }")
|
||||
|
||||
cat /dev/urandom | hexdump -C | grep --line-buffered "$pattern" | \
|
||||
# Monitor /dev/urandom
|
||||
(
|
||||
hexdump -C < /dev/urandom | grep -iF --line-buffered "$pattern" | \
|
||||
while read -r line; do
|
||||
echo $line
|
||||
echo "$line"
|
||||
[[ $delay_ms -gt 0 ]] && sleep "$delay_s"
|
||||
done
|
||||
unset pattern
|
||||
}
|
||||
) & local sub_pid=$!
|
||||
|
||||
IFS= read -r -n 1 -s _ </dev/tty
|
||||
kill -- -"$sub_pid" 2>/dev/null || kill "$sub_pid" 2>/dev/null
|
||||
wait "$sub_pid" 2>/dev/null
|
||||
return 0
|
||||
}
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -36,38 +36,52 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Display list of commands and general informations
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: help
|
||||
help()
|
||||
{
|
||||
cat <<EOF
|
||||
clean Erase backup files
|
||||
dpkgs Search for the given package in the installed ones
|
||||
gpid Give the list of PIDs for the given process name
|
||||
isipv4 Tell if the given IPv4 is valid
|
||||
isipv6 Tell if the given IPv6 is valid
|
||||
ku Kill process owned by users in parameter
|
||||
mcd Create a directory and go inside
|
||||
meteo Display curent weather forecast for the configured city
|
||||
ppg Display process matching the given parameter
|
||||
ppn Display process matching the exact process name given in parameter
|
||||
ppu Display processes owned by the given user
|
||||
rain Let the rain fall
|
||||
rmhost Remove host (IP and/or DNS name) for current known_host
|
||||
rmspc Remove spaces from all the files in working directory
|
||||
setc Set console language to C
|
||||
setfr Set console language to French
|
||||
settrace Activate/deactivate call trace for script debugging
|
||||
setus Set console language to US English
|
||||
showinfo Show the welcoming baner with basic system information
|
||||
ssr Do a root login to the given address
|
||||
taz Compress smartly the given files or directory
|
||||
utaz Uncompress all zip files in the given (or current) directory
|
||||
ver Display version of your copy of profile
|
||||
printf "${BIWhite}Welcome to your profile! Here is a list of available commands:${DEFAULTCOL}\n\n"
|
||||
printf "check_updates\tCheck for new versions of profile\n"
|
||||
printf "clean\t\tErase backup files\n"
|
||||
printf "disp\t\tDisplay formatted info/warning/error/debug messages\n"
|
||||
printf "dwl\t\tDownload a URL to a local file\n"
|
||||
printf "expandlist\tExpand and quote item lists\n"
|
||||
printf "file_stats\tDisplay file size statistics for a path\n"
|
||||
printf "findbig\t\tFind biggest files in the given (or current) directory\n"
|
||||
printf "finddead\tFind dead symbolic links in the given (or current) directory\n"
|
||||
printf "findzero\tFind empty files in the given (or current) directory\n"
|
||||
printf "genpwd\t\tGenerate secure passwords\n"
|
||||
printf "gpid\t\tGive the list of PIDs for the given process name\n"
|
||||
printf "isipv4\t\tTell if the given IPv4 is valid\n"
|
||||
printf "isipv6\t\tTell if the given IPv6 is valid\n"
|
||||
printf "ku\t\tKill process owned by users in parameter\n"
|
||||
printf "matrix\t\tDisplay matrix-style digital rain\n"
|
||||
printf "mcd\t\tCreate a directory and go inside\n"
|
||||
printf "meteo\t\tDisplay current weather forecast for the configured city\n"
|
||||
printf "myextip\tDisplay current external/public IP\n"
|
||||
printf "pkgs\t\tSearch for the given package in installed ones\n"
|
||||
printf "ppg\t\tDisplay process matching the given parameter\n"
|
||||
printf "ppn\t\tDisplay process matching the exact process name given in parameter\n"
|
||||
printf "ppu\t\tDisplay processes owned by the given user\n"
|
||||
printf "profile_upgrade\tUpgrade profile to the latest version\n"
|
||||
printf "rain\t\tLet the rain fall\n"
|
||||
printf "rmhost\t\tRemove host (IP and/or DNS name) from current known_hosts\n"
|
||||
printf "rmspc\t\tRemove spaces from file and directory names\n"
|
||||
printf "setlocale\tSet console language to the current locale\n"
|
||||
printf " * setc\tSet console language to C\n"
|
||||
printf " * setfr\tSet console language to French\n"
|
||||
printf " * setus\tSet console language to US English\n"
|
||||
printf "settrace\tActivate/deactivate call trace for script debugging\n"
|
||||
printf "showinfo\tShow welcome banner with basic system information\n"
|
||||
printf "ssr\t\tDo a root login to the given address\n"
|
||||
printf "taz\t\tCompress smartly the given files or directory\n"
|
||||
printf "urlencode\tURL-encode the given text\n"
|
||||
printf "utaz\t\tUncompress archives in the given (or current) directory\n"
|
||||
printf "ver\t\tDisplay version of your copy of profile\n\n"
|
||||
|
||||
Please use <command> --help to obtain usage details.
|
||||
EOF
|
||||
printf "\nPlease use <command> --help to obtain usage details.\n"
|
||||
}
|
||||
export -f help
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -36,51 +36,136 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Show profile version
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: ver
|
||||
ver()
|
||||
{
|
||||
local PARSED
|
||||
|
||||
PARSED=$(getopt -o h --long help -n 'ver' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"ver --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
|
||||
eval set -- "$PARSED"
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "ver: Display the current profile version.\nUsage: ver\n"
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid options, use \"ver --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[ -z $PROFVERSION ]] && \
|
||||
disp W "No version defined. Profile is probably badly installed." && \
|
||||
return 1
|
||||
disp "Profile version $PROFVERSION."
|
||||
}
|
||||
export -f ver
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Display weather of the given city (or default one)
|
||||
# ------------------------------------------------------------------------------
|
||||
# Display weather for the given city (or the default one)
|
||||
# Usage: meteo [city1 city2 ...]
|
||||
meteo()
|
||||
{
|
||||
local encoded cities=("$@")
|
||||
local PARSED
|
||||
|
||||
PARSED=$(getopt -o h --long help -n 'meteo' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"meteo --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "meteo: Fetch weather data.\n"
|
||||
printf "Usage: meteo [city1 city2 ...]\n"
|
||||
printf "If no city is provided, the default city from configuration will be used.\n"
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid options, use \"meteo --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
local cities=("$@")
|
||||
local city="" encoded=""
|
||||
[[ $# -eq 0 ]] && cities=("$DEFAULT_CITY")
|
||||
|
||||
for city in "${cities[@]}"; do
|
||||
encoded=$(urlencode "$city")
|
||||
curl -s "https://wttr.in/$encoded" || \
|
||||
disp E "Failed fetching datas for $city."
|
||||
dwl "https://wttr.in/$encoded" || \
|
||||
disp E "Failed to fetch weather data for $city."
|
||||
done
|
||||
}
|
||||
export -f meteo
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Display system general information
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: showinfo
|
||||
showinfo()
|
||||
{
|
||||
echo -e "\n"
|
||||
local PARSED
|
||||
|
||||
PARSED=$(getopt -o h --long help -n 'showinfo' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"showinfo --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "showinfo: Display system information (hostname, kernel, uptime and fetch output when available).\n"
|
||||
printf "Usage: showinfo\n"
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid options, use \"showinfo --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
local hostname_str
|
||||
local figopt=()
|
||||
hostname_str="$(hostname)"
|
||||
|
||||
printf "\n"
|
||||
if command -v figlet >/dev/null 2>&1; then
|
||||
if [[ -s /usr/share/figlet/ansi_shadow.flf ]]; then
|
||||
local figopt="-f ansi_shadow"
|
||||
fi
|
||||
if [[ -n $figopt ]]; then
|
||||
figlet -k $figopt $(hostname)
|
||||
[[ -s /usr/share/figlet/ansi_shadow.flf ]] && \
|
||||
figopt=(-f ansi_shadow)
|
||||
figlet -k "${figopt[@]}" "$hostname_str"
|
||||
else
|
||||
figlet $(hostname)
|
||||
printf "%s\n" "$hostname_str"
|
||||
fi
|
||||
else
|
||||
hostname -f
|
||||
fi
|
||||
echo ""
|
||||
|
||||
printf "\n"
|
||||
if command -v neofetch >/dev/null 2>&1; then
|
||||
neofetch
|
||||
elif command -v fastfetch >/dev/null 2>&1; then
|
||||
@@ -90,15 +175,17 @@ showinfo()
|
||||
if [[ -s /etc/os-release ]]; then
|
||||
# shellcheck disable=SC1091
|
||||
. /etc/os-release
|
||||
echo "$NAME $VERSION"
|
||||
printf "%s %s\n" "$NAME" "$VERSION"
|
||||
else
|
||||
cat /proc/version
|
||||
fi
|
||||
echo "Uptime: $(uptime -p)"
|
||||
printf "Uptime: %s\n" "$(uptime -p)"
|
||||
)
|
||||
fi
|
||||
}
|
||||
export -f showinfo
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
load_conf info
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -34,7 +34,8 @@
|
||||
# * OF SUCH DAMAGE.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
locale_check() {
|
||||
locale_check()
|
||||
{
|
||||
locale -a | grep -qx "$1" || {
|
||||
disp W "Locale '$1' is not installed on this system."
|
||||
return 1
|
||||
@@ -45,9 +46,36 @@ locale_check() {
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Change locale to the given one in parameter
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: setlocale <locale>
|
||||
setlocale()
|
||||
{
|
||||
local PARSED
|
||||
PARSED=$(getopt -o h --long help -n 'setlocale' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"setlocale --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "setlocale: Configure system environment locale variables.\n\n"
|
||||
printf "Usage: setlocale <locale>\n\n"
|
||||
printf "Options:\n"
|
||||
printf " -h, --help Display this help screen\n"
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid options, use \"setlocale --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
local loc=$1
|
||||
[[ -z $loc ]] && disp E "No locale specified." && return 1
|
||||
|
||||
@@ -64,11 +92,12 @@ setlocale()
|
||||
disp I "Locale set to $loc."
|
||||
}
|
||||
export -f setlocale
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Special case : change locale to C standard
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: setc
|
||||
setc()
|
||||
{
|
||||
# Locale definitions
|
||||
@@ -76,27 +105,77 @@ setc()
|
||||
disp I "Locale changed to standard C (POSIX)."
|
||||
}
|
||||
export -f setc
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Change locale to French
|
||||
# ------------------------------------------------------------------------------
|
||||
setfr()
|
||||
# Build dynamic locale shortcuts from SET_LOCALE
|
||||
# Expected format:
|
||||
# SET_LOCALE="fr:fr_FR.UTF-8,us:en_US.UTF-8,es:es_ES.UTF-8"
|
||||
# This creates functions:
|
||||
# setfr, setus, setes, ...
|
||||
build_locale_shortcuts()
|
||||
{
|
||||
# Set fr locale definitions
|
||||
setlocale "fr_FR.UTF-8"
|
||||
local cfg="${SET_LOCALE:-}"
|
||||
local item="" alias="" loc="" fname=""
|
||||
local -a locale_items=()
|
||||
|
||||
[[ -z "$cfg" ]] && return 0
|
||||
|
||||
IFS=',' read -r -a locale_items <<< "$cfg"
|
||||
for item in "${locale_items[@]}"; do
|
||||
# Trim surrounding spaces
|
||||
item="${item#"${item%%[![:space:]]*}"}"
|
||||
item="${item%"${item##*[![:space:]]}"}"
|
||||
|
||||
[[ -z "$item" ]] && continue
|
||||
|
||||
if [[ "$item" != *:* ]]; then
|
||||
disp W "Ignoring invalid SET_LOCALE entry: '$item' (expected alias:locale)."
|
||||
continue
|
||||
fi
|
||||
|
||||
alias="${item%%:*}"
|
||||
loc="${item#*:}"
|
||||
|
||||
# Trim alias/locale spaces
|
||||
alias="${alias#"${alias%%[![:space:]]*}"}"
|
||||
alias="${alias%"${alias##*[![:space:]]}"}"
|
||||
loc="${loc#"${loc%%[![:space:]]*}"}"
|
||||
loc="${loc%"${loc##*[![:space:]]}"}"
|
||||
|
||||
# Validate alias for safe function names
|
||||
if [[ ! "$alias" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
|
||||
disp W "Ignoring unsafe locale alias '$alias' in SET_LOCALE."
|
||||
continue
|
||||
fi
|
||||
|
||||
[[ -z "$loc" ]] && {
|
||||
disp W "Ignoring empty locale for alias '$alias' in SET_LOCALE."
|
||||
continue
|
||||
}
|
||||
|
||||
fname="set${alias}"
|
||||
|
||||
# Optional collision warning
|
||||
if declare -F "$fname" >/dev/null 2>&1; then
|
||||
disp W "Overriding existing function '$fname'."
|
||||
fi
|
||||
|
||||
# Build function dynamically
|
||||
# shellcheck disable=SC2016
|
||||
eval "${fname}() { setlocale \"$loc\"; }"
|
||||
# shellcheck disable=SC2163
|
||||
export -f "$fname"
|
||||
done
|
||||
|
||||
unset cfg item alias loc fname locale_items
|
||||
}
|
||||
export -f setfr
|
||||
|
||||
|
||||
export -f build_locale_shortcuts
|
||||
# ------------------------------------------------------------------------------
|
||||
# Change locale to US (needed by Steam)
|
||||
# ------------------------------------------------------------------------------
|
||||
setus()
|
||||
{
|
||||
setlocale "en_US.UTF-8"
|
||||
}
|
||||
export -f setus
|
||||
|
||||
|
||||
load_conf lang
|
||||
build_locale_shortcuts
|
||||
# ------------------------------------------------------------------------------
|
||||
# EOF
|
||||
|
||||
203
profile.d/net.sh
203
profile.d/net.sh
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -35,38 +35,97 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Determine if parameter is a valid IPv4 address
|
||||
# Download a resource using curl, wget, or fetch.
|
||||
# Usage: dwl <url> [output_file]
|
||||
dwl()
|
||||
{
|
||||
case "$1" in
|
||||
--help|-h)
|
||||
echo "Usage: dwl <url> [output_file]"
|
||||
echo "Downloads a resource using curl, wget, or fetch."
|
||||
echo ""
|
||||
echo "Arguments:"
|
||||
echo " url The full URL to download (http/https/ftp)."
|
||||
echo " output_file (Optional) Path to save the file. If omitted, prints to stdout."
|
||||
return 0
|
||||
;;
|
||||
"")
|
||||
echo "Error: URL argument is missing." >&2
|
||||
echo "Try 'get_resource --help' for usage." >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
case "$1" in
|
||||
http://*|https://*|ftp://*) ;;
|
||||
*)
|
||||
echo "Error: '$1' does not look like a valid URL. Must start with http://, https://, or ftp://" >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
local url="$1"
|
||||
local output="$2"
|
||||
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
[ -z "$output" ] && curl -sL "$url" || curl -sL -o "$output" "$url"
|
||||
elif command -v wget >/dev/null 2>&1; then
|
||||
[ -z "$output" ] && wget -qO- "$url" || wget -q -O "$output" "$url"
|
||||
elif command -v fetch >/dev/null 2>&1; then
|
||||
[ -z "$output" ] && fetch -o - "$url" || fetch -o "$output" "$url"
|
||||
else
|
||||
echo "Error: No download utility (curl, wget, or fetch) found." >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
export -f dwl
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Determine if parameter is a valid IPv4 address
|
||||
# Usage: isipv4 <ip_address>
|
||||
isipv4()
|
||||
{
|
||||
# Set up local variables
|
||||
local ip=$1
|
||||
[[ -z $ip ]] && return 1
|
||||
|
||||
# Start with a regex format test
|
||||
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
|
||||
# Start with a regex format test (four octets)
|
||||
if [[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
|
||||
local old_ifs=$IFS
|
||||
IFS="."
|
||||
ip=($ip)
|
||||
IFS='.'
|
||||
read -r -a ip_arr <<< "$ip"
|
||||
IFS=$old_ifs
|
||||
if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 &&
|
||||
${ip[2]} -le 255 && ${ip[3]} -le 255 ]]; then
|
||||
if [[ -t 1 ]]; then
|
||||
disp "The given IPv4 is valid."
|
||||
|
||||
# Ensure each octet is between 0 and 255
|
||||
local oct
|
||||
for oct in "${ip_arr[@]}"; do
|
||||
# Reject leading plus/minus or empty entries
|
||||
if [[ -z $oct || $oct =~ [^0-9] ]]; then
|
||||
[[ -t 1 ]] && disp "The given parameter is NOT a valid IPv4."
|
||||
return 1
|
||||
fi
|
||||
if (( oct > 255 )); then
|
||||
[[ -t 1 ]] && disp "The given parameter is NOT a valid IPv4."
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
|
||||
[[ -t 1 ]] && disp "The given IPv4 is valid."
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
if [[ -t 1 ]]; then
|
||||
disp "The given parameter is NOT a valid IPv4."
|
||||
fi
|
||||
|
||||
[[ -t 1 ]] && disp "The given parameter is NOT a valid IPv4."
|
||||
return 1
|
||||
}
|
||||
export -f isipv4
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Determine if parameter is a valid IPv4 address
|
||||
# ------------------------------------------------------------------------------
|
||||
# Determine if parameter is a valid IPv6 address
|
||||
# Usage: isipv6 <ip_address>
|
||||
isipv6()
|
||||
{
|
||||
local ip="$1"
|
||||
@@ -83,10 +142,12 @@ isipv6()
|
||||
return 1
|
||||
}
|
||||
export -f isipv6
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Encode a string so it can be used as a URL parameter
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: urlencode <string>
|
||||
urlencode() {
|
||||
local LANG=C
|
||||
local str="$*"
|
||||
@@ -94,13 +155,117 @@ urlencode() {
|
||||
for (( i = 0; i < length; i++ )); do
|
||||
local c="${str:i:1}"
|
||||
case "$c" in
|
||||
[a-zA-Z0-9.~_-]) printf "$c" ;;
|
||||
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
|
||||
' ') printf '+' ;;
|
||||
*) printf '%%%02X' "'$c" #| cut -d' ' -f2 ;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
export -f urlencode
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Fetch and display external IP information
|
||||
# Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord]
|
||||
# If no option is provided, all information will be displayed.
|
||||
# Options:
|
||||
# -h, --help Display help screen
|
||||
# -i, --ip Display only the external IP address
|
||||
# -s, --isp Display only the ISP name
|
||||
# -l, --loc Display only the location (city, region, country)
|
||||
# -c, --coord Display only the coordinates (latitude, longitude)
|
||||
# -a, --as Display only the Autonomous System (AS) information
|
||||
# -R, --raw Display raw JSON response
|
||||
myextip() {
|
||||
local show_ip=false show_isp=false show_loc=false
|
||||
local show_coord=false show_as=false show_raw=false
|
||||
local all=true
|
||||
|
||||
# Parse arguments
|
||||
while [[ "$#" -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-i|--ip)
|
||||
show_ip=true
|
||||
all=false
|
||||
;;
|
||||
-s|--isp)
|
||||
show_isp=true
|
||||
all=false
|
||||
;;
|
||||
-l|--loc)
|
||||
show_loc=true
|
||||
all=false
|
||||
;;
|
||||
-c|--coord)
|
||||
show_coord=true
|
||||
all=false
|
||||
;;
|
||||
-a|--as)
|
||||
show_as=true
|
||||
all=false
|
||||
;;
|
||||
-R|--raw)
|
||||
all=false
|
||||
show_raw=true
|
||||
;;
|
||||
-h|--help)
|
||||
printf "Fetch and display external IP information.\n\n"
|
||||
printf "Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord] [-a|--as] [-R|--raw]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\tDisplay this help screen\n"
|
||||
printf "\t-i, --ip\tDisplay only the external IP address\n"
|
||||
printf "\t-s, --isp\tDisplay only the ISP name\n"
|
||||
printf "\t-l, --loc\tDisplay only the location (city, region, country)\n"
|
||||
printf "\t-c, --coord\tDisplay only the coordinates (latitude, longitude)\n"
|
||||
printf "\t-a, --as\tDisplay only the Autonomous System (AS) information\n"
|
||||
printf "\t-R, --raw\tDisplay raw JSON response\n"
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Unknown option: $1, use \"myextip --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
# Fetch data. Allow overriding endpoint via env var MYEXTIP_URL
|
||||
local MYEXTIP_URL
|
||||
MYEXTIP_URL=${MYEXTIP_URL:-http://ip-api.com/json/}
|
||||
|
||||
local response
|
||||
if ! response=$(dwl "$MYEXTIP_URL"); then
|
||||
disp E "Failed to fetch external IP information from $MYEXTIP_URL"
|
||||
return 2
|
||||
fi
|
||||
|
||||
# Parse with jq when available and when raw wasn't requested. The jq filter
|
||||
# is tolerant to field-name differences between providers (ip-api / ipinfo).
|
||||
if command -v jq >/dev/null 2>&1 && [[ "$show_raw" != true ]]; then
|
||||
echo "$response" | jq -r --argjson all "$all" --argjson ip "$show_ip" \
|
||||
--argjson isp "$show_isp" --argjson loc "$show_loc" \
|
||||
--argjson coord "$show_coord" --argjson as "$show_as" '
|
||||
[
|
||||
(if $all or $ip then "IP Address : \(.query // .ip)" else empty end),
|
||||
(if $all or $isp then "ISP : \(.isp // .org)" else empty end),
|
||||
(if $all or $loc then
|
||||
("Location : " + ((.city // "") + (if .city then ", " else "" end) + (if .regionName then .regionName else .region end) + (if .country then ", " + .country else "" end)))
|
||||
else empty end),
|
||||
(if $all or $coord then (if (.lat and .lon) then "Coordinates: \(.lat), \(.lon)" elif .loc then "Coordinates: \(.loc)" else empty end) else empty end),
|
||||
(if $all or $as then "AS : \(.as // .org)" else empty end)
|
||||
] | .[]'
|
||||
else
|
||||
[[ "$show_raw" != true ]] && disp W "jq is not installed, displaying raw JSON response."
|
||||
echo "$response"
|
||||
fi
|
||||
}
|
||||
export -f myextip
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -36,38 +36,53 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Look for a package within installed one
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: dpkgs <string>
|
||||
pkgs()
|
||||
{
|
||||
local count=0
|
||||
for opt in $@; do
|
||||
case $opt in
|
||||
"-h" | "--help")
|
||||
echo "dpkgs: look for an installed package by it's name."
|
||||
echo
|
||||
echo "Usage: dpkgs <string>"
|
||||
local ignore_case=0
|
||||
|
||||
local PARSED
|
||||
PARSED=$(getopt -o hi --long help,ignore-case -n 'pkgs' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"pkgs --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "pkgs: Look for an installed package by its name.\n\n"
|
||||
printf "Usage: pkgs [options] <string>\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\tDisplay this help screen\n"
|
||||
printf "\t-i, --ignore-case\tIgnore case distinctions\n"
|
||||
return 0
|
||||
;;
|
||||
|
||||
"-"*)
|
||||
disp E "Invalid option, use \"dpkgs --help\" to display usage."
|
||||
echo
|
||||
return 1
|
||||
-i|--ignore-case)
|
||||
ignore_case=1
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
|
||||
*)
|
||||
local pkg=$1 && shift
|
||||
count=$(($count + 1))
|
||||
[[ $count -gt 1 ]] &&
|
||||
disp E "Please specify a package name, without space, eventually partial." &&
|
||||
disp E "Invalid option: $1"
|
||||
return 1
|
||||
|
||||
;;
|
||||
esac
|
||||
done
|
||||
[[ $count -lt 1 ]] &&
|
||||
disp E "Please specify a package name, without space, eventually partial." &&
|
||||
|
||||
local pkg="$1"
|
||||
[[ -z "$pkg" ]] && {
|
||||
disp E "Please specify a package name, without space, eventually partial."
|
||||
return 1
|
||||
}
|
||||
|
||||
# Build grep command
|
||||
local grep_opt=""
|
||||
(( ignore_case )) && grep_opt="-i"
|
||||
|
||||
command -v dpkg >/dev/null 2>&1 && local cmd="dpkg -l"
|
||||
command -v rpm >/dev/null 2>&1 && local cmd="rpm -qa"
|
||||
@@ -75,9 +90,10 @@ pkgs()
|
||||
disp E "No usable package manager seems unavialable."
|
||||
return 2
|
||||
fi
|
||||
$cmd | grep $pkg
|
||||
$cmd | grep $grep_opt $pkg
|
||||
}
|
||||
export -f pkgs
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -36,18 +36,38 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Search processes matching the given string
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: ppg <string>
|
||||
ppg()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
printf "ppg: Search processes matching the given string.\n\n"
|
||||
printf "Usage: ppg <string>\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
return 0
|
||||
fi
|
||||
if [[ -z "$1" ]]; then
|
||||
disp E "Usage: ppg <string>"
|
||||
return 1
|
||||
fi
|
||||
ps -edf | grep "$@" | grep -v "grep $@"
|
||||
}
|
||||
export -f ppg
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# List processes owned by a specific user
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: ppu <username>
|
||||
ppu()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
printf "ppu: List processes owned by a specific user.\n\n"
|
||||
printf "Usage: ppu <username>\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
return 0
|
||||
fi
|
||||
if [[ -z "$1" ]]; then
|
||||
disp E "Usage: ppu <username>"
|
||||
return 1
|
||||
@@ -58,12 +78,21 @@ ppu()
|
||||
ps -u "$1" -o pid,user,%cpu,%mem,start,time,command
|
||||
}
|
||||
export -f ppu
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# List processes by exact command name (no path/parameters)
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: ppn <command_name>
|
||||
ppn()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
printf "ppn: List processes by exact command name (no path/parameters).\n\n"
|
||||
printf "Usage: ppn <command_name>\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
return 0
|
||||
fi
|
||||
if [[ -z "$1" ]]; then
|
||||
disp E "Usage: ppn <command_name>"
|
||||
return 1
|
||||
@@ -75,12 +104,25 @@ ppn()
|
||||
ps -eo pid,comm | grep -w "$1"
|
||||
}
|
||||
export -f ppn
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Get PID list of the given process name
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: ppid <process_name [process_name2 ...]>
|
||||
gpid()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
printf "gpid: Get PID list of the given process name.\n\n"
|
||||
printf "Usage: gpid <process_name [process_name2 ...]>\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
return 0
|
||||
fi
|
||||
if [[ -z "$1" ]]; then
|
||||
disp E "Usage: gpid <process_name [process_name2 ...]>"
|
||||
return 1
|
||||
fi
|
||||
[[ $UID -eq 0 ]] && local psopt="-A"
|
||||
[[ $# -eq 1 ]] && local single=1
|
||||
for pid in $@; do
|
||||
@@ -94,34 +136,65 @@ gpid()
|
||||
[[ $result ]] || return 1
|
||||
}
|
||||
export -f gpid
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Kill all processes owned by the given users (kill user)
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: ku <username1 [username2 ...]>
|
||||
ku()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
printf "ku: Kill all processes owned by the given users.\n\n"
|
||||
printf "Usage: ku <username1 [username2 ...]>\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
return 0
|
||||
fi
|
||||
if [[ -z "$1" ]]; then
|
||||
disp E "Usage: ku <username1 [username2 ...]>"
|
||||
return 1
|
||||
fi
|
||||
for u in $@; do
|
||||
killall -u "$u"
|
||||
done
|
||||
}
|
||||
export -f ku
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Kill all children of a process then the process (kill tree)
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: kt <pid> [kill_options]
|
||||
kt()
|
||||
{
|
||||
[[ -z $1 ]] && echo -e "Usage:\n\tkt <pid> [kill_options]"
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
printf "kt: Kill all children of a process then the process (kill tree).\n\n"
|
||||
printf "Usage: kt <pid> [kill_options]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
return 0
|
||||
fi
|
||||
if [[ -z "$1" ]]; then
|
||||
disp E "Usage: ppg <string>"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local parent_pid="$1"
|
||||
shift
|
||||
if [[ "$parent_pid" == "0" || "$parent_pid" == "1" ]]; then
|
||||
disp E "Safety abort: Refusing to kill PID $parent_pid (system critical)."
|
||||
return 1
|
||||
fi
|
||||
|
||||
children_pids=$(pgrep -P "$parent_pid")
|
||||
|
||||
for pid in $children_pids; do
|
||||
kt "$pid" "$@"
|
||||
kt "$pid" "$@" || break
|
||||
done
|
||||
kill "$@" "$parent_pid"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -35,17 +36,26 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# timer_* functions : internal timing function for prompt
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: timer_now
|
||||
# This function returns the current time in nanoseconds since the epoch. It
|
||||
# first tries to use the %N format specifier for nanoseconds, but if that is
|
||||
# not supported (e.g., on older systems), it falls back to seconds.
|
||||
function timer_now
|
||||
{
|
||||
date +%s%N 2>/dev/null || date +%s
|
||||
}
|
||||
|
||||
# Usage: timer_start
|
||||
# This function initializes the timer_start variable with the current time in
|
||||
# nanoseconds. It is used to measure the elapsed time for the prompt.
|
||||
function timer_start
|
||||
{
|
||||
timer_start=${timer_start:-$(timer_now)}
|
||||
}
|
||||
|
||||
# Usage: timer_stop
|
||||
# This function calculates the elapsed time since timer_start and formats it
|
||||
# into a human-readable string with appropriate units (us, ms, s, m, h
|
||||
function timer_stop
|
||||
{
|
||||
local delta_us=$((($(timer_now) - $timer_start) / 1000))
|
||||
@@ -74,8 +84,11 @@ function timer_stop
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Function triguered internaly by bash : defining prompt
|
||||
# ------------------------------------------------------------------------------
|
||||
# Function triggered internally by bash : defining prompt
|
||||
# Usage: set_prompt
|
||||
# This function is called by bash before displaying the prompt. It sets the
|
||||
# PS1 variable to a custom prompt that includes the exit status of the last
|
||||
# command, the elapsed time of the last command, and the current user and host.
|
||||
set_prompt()
|
||||
{
|
||||
local Last_Command=$? # Must come first!
|
||||
@@ -123,6 +136,7 @@ set_prompt()
|
||||
# the text color to the default.
|
||||
PS1+="$ICyan\\w \\\$$Default "
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
117
profile.d/pwd.sh
117
profile.d/pwd.sh
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -36,9 +36,20 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# genpwd : generate a password with different criteria
|
||||
# default 16 car with up and low car, symbol and number
|
||||
# Usage: genpwd [options] [--extracars=<cars>] [--length=<n>] [nb_passwd]
|
||||
# Options:
|
||||
# -h, --help Display that help screen
|
||||
# -s, --nosymbols Exclude symbols
|
||||
# -n, --nonumbers Exclude numbers
|
||||
# -u, --noup Exclude uppercase letters
|
||||
# -l, --nolow Exclude lowercase letters
|
||||
# -e=<c>, --extracars=<c>
|
||||
# Add the given caracters to the possible caracter list
|
||||
# -L=<n>, --length=<n>
|
||||
# Set length of the password (default is 16)
|
||||
# -o=<n>, --occurences=<n>
|
||||
# Set the maximum occurences of a same caracter (default is 2)
|
||||
# The function is very slow on Windows
|
||||
# ------------------------------------------------------------------------------
|
||||
genpwd()
|
||||
{
|
||||
local length=16
|
||||
@@ -47,80 +58,81 @@ genpwd()
|
||||
local nbpwd=1
|
||||
local extcar
|
||||
|
||||
for opt in $@; do
|
||||
case $opt in
|
||||
"-h" | "--help")
|
||||
echo "genpwd: generate one or more secure random password."
|
||||
echo
|
||||
echo "Usage: genpwd [options] [--extracars=<cars>] [--length=<n>] [nb_passwd]"
|
||||
echo
|
||||
echo "Options:"
|
||||
echo " -h, --help Display that help screen"
|
||||
echo " -s, --nosymbols Exclude symbols"
|
||||
echo " -n, --nonumbers Exclude numbers"
|
||||
echo " -u, --noup Exclude uppercase letters"
|
||||
echo " -l, --nolow Exclude lowercase letters"
|
||||
echo " -e=<c>, --extracars=<c>"
|
||||
echo " Add the given caracters to the possible caracter list"
|
||||
echo " -L=<n>, --length=<n>"
|
||||
echo " Set length of the password (default is $length)"
|
||||
echo " -o=<n>, --occurences=<n>"
|
||||
echo " Set the maximum occurences of a same caracter (default is $occurs)"
|
||||
echo
|
||||
echo "If the --extracars parameter is given, at least one of the given caracter will"
|
||||
echo "be used in the final password."
|
||||
echo
|
||||
echo "Please note that some caracters might be interpreted by Bash or Awk programs,"
|
||||
echo "and thus, cannot be used without provoquing errors. Those identified caracters"
|
||||
echo "are :"
|
||||
echo ' * ? \ $ { }'
|
||||
echo
|
||||
local PARSED
|
||||
PARSED=$(getopt -o hsnu l e:L:o: --long \
|
||||
help,nosymbols,nonumbers,noup,nolow,extracars:,length:,occurences: -n 'genpwd' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then return 1; fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "genpwd: Generate secure random password(s).\n\n"
|
||||
printf "Usage: genpwd [options] [nb_passwd]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
printf "\t-s, --nosymbols\t\tExclude symbols\n"
|
||||
printf "\t-n, --nonumbers\t\tExclude numbers\n"
|
||||
printf "\t-u, --noup\t\tExclude uppercase letters\n"
|
||||
printf "\t-l, --nolow\t\tExclude lowercase letters\n"
|
||||
printf "\t-e, --extracars <c>\tAdd characters to list\n"
|
||||
printf "\t-L, --length <n>\tSet password length (default: 16)\n"
|
||||
printf "\t-o, --occurences <n>\tMax occurences per character (default: 2)\n"
|
||||
return 0
|
||||
;;
|
||||
"-s" | "--nosymbols")
|
||||
-s|--nosymbols)
|
||||
symb=0
|
||||
shift
|
||||
;;
|
||||
"-n" | "--nonumbers")
|
||||
-n|--nonumbers)
|
||||
numb=0
|
||||
shift
|
||||
;;
|
||||
"-u" | "--noup")
|
||||
-u|--noup)
|
||||
maj=0
|
||||
shift
|
||||
;;
|
||||
"-l" | "--nolow")
|
||||
-l|--nolow)
|
||||
min=0
|
||||
shift
|
||||
;;
|
||||
"-e"?* | "--extracars"?*)
|
||||
extcar=$(echo "$opt" | cut -f 2- -d '=')
|
||||
-e|--extracars)
|
||||
extcar="$2"
|
||||
shift 2
|
||||
;;
|
||||
"-L"?* | "--length"?*)
|
||||
local length=$(echo "$opt" | cut -f 2- -d '=')
|
||||
-L|--length)
|
||||
length="$2"
|
||||
if ! [[ $length =~ ^[0-9]+$ ]]; then
|
||||
disp E "The --length parameter requires a number."
|
||||
return 1
|
||||
fi
|
||||
shift 2
|
||||
;;
|
||||
"-o"?* | "--occurences"?*)
|
||||
local occurs=$(echo "$opt" | cut -f 2- -d '=')
|
||||
-o|--occurences)
|
||||
occurs="$2"
|
||||
if ! [[ $occurs =~ ^[1-9]+$ ]]; then
|
||||
disp E "The --occurs parameter requires a number from 1 to 9."
|
||||
return 1
|
||||
fi
|
||||
shift 2
|
||||
;;
|
||||
"-*")
|
||||
disp E "Unknow parameter ${opt}."
|
||||
return 1
|
||||
--)
|
||||
shift; break
|
||||
;;
|
||||
*)
|
||||
if ! [[ $opt =~ ^[1-9]+$ ]]; then
|
||||
disp E "Unknow parameter ${opt}."
|
||||
return 1
|
||||
else
|
||||
nbpwd=$opt
|
||||
fi
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -n "$1" ]]; then
|
||||
nbpwd="$1"
|
||||
if ! [[ $nbpwd =~ ^[0-9]+$ ]]; then
|
||||
disp E "The number of password to generate must be a number."
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Function selecting a random caracter from the list in parameter
|
||||
pickcar() {
|
||||
# When a character is picked we check if it's not appearing already twice
|
||||
@@ -136,7 +148,7 @@ genpwd()
|
||||
}
|
||||
|
||||
disp I "Generating $nbpwd passwords, please wait..."
|
||||
for n in $(seq 1 $nbpwd); do
|
||||
for (( n=1; n<=nbpwd; n++ )); do
|
||||
{
|
||||
local carset='' # store final caracter set to use
|
||||
local picked='' # store already used caracter
|
||||
@@ -185,6 +197,7 @@ genpwd()
|
||||
done
|
||||
}
|
||||
export -f genpwd
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -35,166 +35,221 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Let the rain fall
|
||||
# ------------------------------------------------------------------------------
|
||||
rain()
|
||||
# Generic rain-like engine and presets
|
||||
|
||||
_rain_build_colors()
|
||||
{
|
||||
show_usage() {
|
||||
echo -e "Usage: rain [OPTIONS]"
|
||||
echo -e ""
|
||||
echo -e "Options:"
|
||||
echo -e " -s, --speed NUM Set the drop delay in seconds (default: 0.050)."
|
||||
echo -e " Lower values = faster rain."
|
||||
echo -e " -c, --color COLOR Set the color theme (default: white)."
|
||||
echo -e " -h, --help Display this help message and exit."
|
||||
echo -e ""
|
||||
echo -e "Available Colors:"
|
||||
echo -e " \e[32mgreen\e[0m : The classic Matrix digital rain"
|
||||
echo -e " \e[34mblue\e[0m : Deep ocean blue gradients"
|
||||
echo -e " \e[31mred\e[0m : Crimson/Blood rain"
|
||||
echo -e " \e[33myellow\e[0m : Amber and gold tones"
|
||||
echo -e " \e[36mcyan\e[0m : Electric cyan/turquoise"
|
||||
echo -e " white : Greyscale and white (original style)"
|
||||
echo -e ""
|
||||
echo -e "Example: rain --color green --speed 0.03"
|
||||
}
|
||||
local base_color="$1"
|
||||
RAIN_ENGINE_COLORS=()
|
||||
|
||||
local step_duration=0.050
|
||||
local base_color="white" # default color scheme, can be overridden by --color
|
||||
case $base_color in
|
||||
green)
|
||||
for i in {22..28} {34..40} {46..48}; do RAIN_ENGINE_COLORS+=("\e[38;5;${i}m"); done ;;
|
||||
blue)
|
||||
for i in {17..21} {27..33} {39..45}; do RAIN_ENGINE_COLORS+=("\e[38;5;${i}m"); done ;;
|
||||
red)
|
||||
for i in {52..52} {88..88} {124..124} {160..160} {196..201}; do RAIN_ENGINE_COLORS+=("\e[38;5;${i}m"); done ;;
|
||||
yellow)
|
||||
for i in {58..58} {100..100} {142..142} {184..184} {226..229}; do RAIN_ENGINE_COLORS+=("\e[38;5;${i}m"); done ;;
|
||||
cyan)
|
||||
for i in {30..31} {37..38} {44..45} {50..51}; do RAIN_ENGINE_COLORS+=("\e[38;5;${i}m"); done ;;
|
||||
*)
|
||||
RAIN_ENGINE_COLORS=("\e[37m" "\e[37;1m")
|
||||
for i in {244..255}; do RAIN_ENGINE_COLORS+=("\e[38;5;${i}m"); done ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Analyse arguments
|
||||
while [[ "$#" -gt 0 ]]; do
|
||||
case $1 in
|
||||
-s|--speed)
|
||||
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
|
||||
step_duration="$2"; shift
|
||||
else
|
||||
echo -e "\e[31mError: --speed requires a numeric value.\e[0m"
|
||||
show_usage && return 1
|
||||
fi
|
||||
_rain_build_chars()
|
||||
{
|
||||
local mode="$1"
|
||||
local charset="$2"
|
||||
RAIN_ENGINE_CHARS=()
|
||||
|
||||
case "$mode" in
|
||||
matrix)
|
||||
case "$charset" in
|
||||
""|binary)
|
||||
RAIN_ENGINE_CHARS=("0" "1")
|
||||
;;
|
||||
-c|--color)
|
||||
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
|
||||
base_color="$2"; shift
|
||||
else
|
||||
echo -e "\e[31mError: --color requires a color name.\e[0m"
|
||||
show_usage && return 1
|
||||
fi
|
||||
kana|kanji)
|
||||
# Half-width katakana set, generally rendered as single-cell glyphs.
|
||||
RAIN_ENGINE_CHARS=("ア" "イ" "ウ" "エ" "オ" "カ" "キ" "ク" "ケ" "コ" "サ" "シ" "ス" "セ" "ソ" "タ" "チ" "ツ" "テ" "ト" "ナ" "ニ" "ヌ" "ネ" "ノ" "ハ" "ヒ" "フ" "ヘ" "ホ" "マ" "ミ" "ム" "メ" "モ" "ヤ" "ユ" "ヨ" "ラ" "リ" "ル" "レ" "ロ" "ワ" "ン")
|
||||
;;
|
||||
-h|--help)
|
||||
show_usage && return 0
|
||||
ascii)
|
||||
RAIN_ENGINE_CHARS=("0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F")
|
||||
;;
|
||||
*)
|
||||
echo -e "\e[31mUnknown option: $1\e[0m"
|
||||
show_usage && return 1
|
||||
disp E "Unknown charset: ${charset} (supported: binary, kana, ascii)."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
# Define colors (256-colors gradients)
|
||||
local rain_colors=()
|
||||
case $base_color in
|
||||
green) # Matrix style green
|
||||
for i in {22..28} {34..40} {46..48}; do rain_colors+=("\e[38;5;${i}m"); done ;;
|
||||
blue) # Deep ocean blues
|
||||
for i in {17..21} {27..33} {39..45}; do rain_colors+=("\e[38;5;${i}m"); done ;;
|
||||
red) # Crimson / blood red
|
||||
for i in {52..52} {88..88} {124..124} {160..160} {196..201}; do rain_colors+=("\e[38;5;${i}m"); done ;;
|
||||
yellow) # Amber / gold
|
||||
for i in {58..58} {100..100} {142..142} {184..184} {226..229}; do rain_colors+=("\e[38;5;${i}m"); done ;;
|
||||
cyan) # Electric cyan / turquoise
|
||||
for i in {30..31} {37..38} {44..45} {50..51}; do rain_colors+=("\e[38;5;${i}m"); done ;;
|
||||
*) # Greyscale / white (original style)
|
||||
rain_colors=("\e[37m" "\e[37;1m")
|
||||
for i in {244..255}; do rain_colors+=("\e[38;5;${i}m"); done ;;
|
||||
;;
|
||||
*)
|
||||
RAIN_ENGINE_CHARS=("|" "│" "┃" "┆" "┇" "┊" "┋" "╽" "╿")
|
||||
;;
|
||||
esac
|
||||
|
||||
local exit_st=0
|
||||
local rain_cars=("|" "│" "┃" "┆" "┇" "┊" "┋" "╽" "╿")
|
||||
local rain_tab=${#rain_cars[@]}
|
||||
local rain_color_tab=${#rain_colors[@]}
|
||||
local num_rain_metadata=5
|
||||
local term_height=$(tput lines)
|
||||
local term_width=$(tput cols)
|
||||
local X=0 Y=0 drop_length=0 rain_drop=0
|
||||
local max_rain_width=0 new_rain_odd=0 falling_odd=0
|
||||
return 0
|
||||
}
|
||||
|
||||
sigwinch() {
|
||||
_rain_normalize_speed()
|
||||
{
|
||||
local raw_speed="$1"
|
||||
|
||||
# Accept integer/floating values. UI scale is centiseconds by default:
|
||||
# 5 -> 0.05s, 2.5 -> 0.025s. Values < 1 are treated as direct seconds
|
||||
# for backward compatibility (e.g. 0.03).
|
||||
if [[ ! "$raw_speed" =~ ^[0-9]+([.][0-9]+)?$ ]]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
if awk -v s="$raw_speed" 'BEGIN { exit !(s < 1) }'; then
|
||||
printf "%s" "$raw_speed"
|
||||
else
|
||||
awk -v s="$raw_speed" 'BEGIN { printf "%.3f", s / 100 }'
|
||||
fi
|
||||
}
|
||||
|
||||
_rain_engine()
|
||||
{
|
||||
local step_duration="$1"
|
||||
local base_color="$2"
|
||||
local mode="$3"
|
||||
local charset="$4"
|
||||
|
||||
command -v tput >/dev/null 2>&1 || {
|
||||
disp E "The program 'tput' is required but not installed."
|
||||
return 1
|
||||
}
|
||||
|
||||
_rain_build_colors "$base_color"
|
||||
_rain_build_chars "$mode" "$charset" || return 1
|
||||
|
||||
local rain_colors=("${RAIN_ENGINE_COLORS[@]}")
|
||||
local rain_chars=("${RAIN_ENGINE_CHARS[@]}")
|
||||
local rain_color_tab=${#rain_colors[@]}
|
||||
local rain_tab=${#rain_chars[@]}
|
||||
local matrix_head_color=$'\e[1;97m'
|
||||
|
||||
local exit_st=0
|
||||
local num_rain_metadata=5
|
||||
local term_height=0 term_width=0
|
||||
local X=0 Y=0 drop_length=0 rain_drop=0
|
||||
local max_rain_width=0 max_rain_height=0
|
||||
local new_rain_odd=0 falling_odd=0
|
||||
local term_area=0
|
||||
local frame_sleep="$step_duration"
|
||||
|
||||
sigwinch()
|
||||
{
|
||||
term_width=$(tput cols)
|
||||
term_height=$(tput lines)
|
||||
#step_duration=0.025
|
||||
((max_rain_width = term_width * term_height / 4))
|
||||
((term_area = term_width * term_height))
|
||||
|
||||
case "$mode" in
|
||||
matrix)
|
||||
((max_rain_width = term_area / 3))
|
||||
((max_rain_height = term_height < 8 ? 1 : term_height / 6))
|
||||
((new_rain_odd = term_height > 50 ? 100 : term_height * 2))
|
||||
((new_rain_odd = new_rain_odd * 85 / 100))
|
||||
((falling_odd = 100))
|
||||
|
||||
# Adapt cadence and density to terminal size for smoother rendering.
|
||||
if ((term_area < 1200)); then
|
||||
((max_rain_width = term_area / 4))
|
||||
frame_sleep=$(awk -v s="$step_duration" 'BEGIN { printf "%.3f", s * 1.15 }')
|
||||
elif ((term_area > 5000)); then
|
||||
((max_rain_width = term_area / 2))
|
||||
frame_sleep=$(awk -v s="$step_duration" 'BEGIN { printf "%.3f", s * 0.85 }')
|
||||
else
|
||||
frame_sleep="$step_duration"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
((max_rain_width = term_area / 4))
|
||||
((max_rain_height = term_height < 10 ? 1 : term_height / 10))
|
||||
# In percentage
|
||||
((new_rain_odd = term_height > 50 ? 100 : term_height * 2))
|
||||
((new_rain_odd = new_rain_odd * 75 / 100))
|
||||
((falling_odd = term_height > 25 ? 100 : term_height * 4))
|
||||
((falling_odd = falling_odd * 90 / 100))
|
||||
frame_sleep="$step_duration"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
do_exit() {
|
||||
do_exit()
|
||||
{
|
||||
exit_st=1
|
||||
}
|
||||
|
||||
do_render() {
|
||||
# Clean screen first
|
||||
local idx=0
|
||||
do_render()
|
||||
{
|
||||
local idx=0 y=0 drop_color="" current_char="" render_color=""
|
||||
|
||||
for ((idx = 0; idx < num_rains * num_rain_metadata; idx += num_rain_metadata)); do
|
||||
X=${rains[idx]}
|
||||
Y=${rains[idx + 1]}
|
||||
drop_length=${rains[idx + 4]}
|
||||
for ((y = Y; y < Y + drop_length; y++)); do
|
||||
((y < 1 || y > term_height)) && continue
|
||||
echo -ne "\e[${y};${X}H "
|
||||
printf "\e[%d;%dH " "$y" "$X"
|
||||
done
|
||||
done
|
||||
|
||||
for ((idx = 0; idx < num_rains * num_rain_metadata; idx += num_rain_metadata)); do
|
||||
if ((100 * RANDOM / 32768 < falling_odd)); then
|
||||
# Falling
|
||||
if ((++rains[idx + 1] > term_height)); then
|
||||
# Out of screen, bye sweet <3
|
||||
rains=("${rains[@]:0:idx}"
|
||||
"${rains[@]:idx+num_rain_metadata:num_rains*num_rain_metadata}")
|
||||
rains=("${rains[@]:0:idx}" "${rains[@]:idx+num_rain_metadata:num_rains*num_rain_metadata}")
|
||||
((num_rains--))
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
|
||||
X=${rains[idx]}
|
||||
Y=${rains[idx + 1]}
|
||||
rain_drop=${rains[idx + 2]}
|
||||
drop_color=${rains[idx + 3]}
|
||||
drop_length=${rains[idx + 4]}
|
||||
|
||||
for ((y = Y; y < Y + drop_length; y++)); do
|
||||
((y < 1 || y > term_height)) && continue
|
||||
echo -ne "\e[${y};${X}H${drop_color}${rain_drop}"
|
||||
if [[ "$mode" == "matrix" ]]; then
|
||||
current_char="${rain_chars[rain_tab * RANDOM / 32768]}"
|
||||
if ((y == Y + drop_length - 1)); then
|
||||
render_color="$matrix_head_color"
|
||||
else
|
||||
render_color="$drop_color"
|
||||
fi
|
||||
else
|
||||
current_char="$rain_drop"
|
||||
render_color="$drop_color"
|
||||
fi
|
||||
printf "\e[%d;%dH%b%s" "$y" "$X" "$render_color" "$current_char"
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
trap do_exit TERM INT
|
||||
trap sigwinch WINCH
|
||||
# No echo stdin and hide the cursor
|
||||
stty -echo
|
||||
echo -ne "\e[?25l"
|
||||
printf "\e[?25l"
|
||||
printf "\e[2J"
|
||||
|
||||
echo -ne "\e[2J"
|
||||
local rains=()
|
||||
local num_rains=0
|
||||
local ch=""
|
||||
|
||||
sigwinch
|
||||
while ((exit_st <= 0)); do
|
||||
if (($exit_st <= 0)); then
|
||||
read -n 1 -t $step_duration ch
|
||||
read -r -n 1 -t "$frame_sleep" ch
|
||||
case "$ch" in
|
||||
q | Q)
|
||||
q|Q)
|
||||
do_exit
|
||||
;;
|
||||
esac
|
||||
|
||||
if ((num_rains < max_rain_width)) && ((100 * RANDOM / 32768 < new_rain_odd)); then
|
||||
# Need new |, 1-based
|
||||
rain_drop="${rain_cars[rain_tab * RANDOM / 32768]}"
|
||||
rain_drop="${rain_chars[rain_tab * RANDOM / 32768]}"
|
||||
drop_color="${rain_colors[rain_color_tab * RANDOM / 32768]}"
|
||||
drop_length=$((max_rain_height * RANDOM / 32768 + 1))
|
||||
X=$((term_width * RANDOM / 32768 + 1))
|
||||
@@ -203,20 +258,176 @@ rain()
|
||||
((num_rains++))
|
||||
fi
|
||||
|
||||
# Let rain fall!
|
||||
do_render
|
||||
fi
|
||||
done
|
||||
echo -ne "\e[${term_height};1H\e[0K"
|
||||
|
||||
# Show cursor and echo stdin
|
||||
echo -ne "\e[?25h"
|
||||
printf "\e[%d;1H\e[0K" "$term_height"
|
||||
printf "\e[?25h"
|
||||
stty echo
|
||||
unset exit_st
|
||||
trap - TERM INT
|
||||
trap - WINCH
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Let the rain fall (current style)
|
||||
# Usage: rain [OPTIONS]
|
||||
rain()
|
||||
{
|
||||
_rain_show_usage()
|
||||
{
|
||||
printf "Usage: rain [OPTIONS]\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-s, --speed NUM Set speed value (default: 5 => 0.050s).\n"
|
||||
printf "\t Values >=1 use a /100 scale (5 => 0.05s).\n"
|
||||
printf "\t Values <1 are interpreted as raw seconds.\n"
|
||||
printf "\t-c, --color COLOR Set the color theme (default: white).\n"
|
||||
printf "\t-h, --help Display this help message and exit.\n\n"
|
||||
printf "Available Colors:\n"
|
||||
printf "\t\e[32mgreen\e[0m\t: Matrix-like green shades\n"
|
||||
printf "\t\e[34mblue\e[0m\t: Deep ocean blue gradients\n"
|
||||
printf "\t\e[31mred\e[0m\t: Crimson/Blood rain\n"
|
||||
printf "\t\e[33myellow\e[0m\t: Amber and gold tones\n"
|
||||
printf "\t\e[36mcyan\e[0m\t: Electric cyan/turquoise\n"
|
||||
printf "\twhite\t: Greyscale and white (original style)\n\n"
|
||||
printf "Example: rain --color green --speed 3\n"
|
||||
}
|
||||
|
||||
local step_duration=0.050
|
||||
local base_color="white"
|
||||
|
||||
while [[ "$#" -gt 0 ]]; do
|
||||
case $1 in
|
||||
-s|--speed)
|
||||
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
|
||||
step_duration=$(_rain_normalize_speed "$2") || {
|
||||
disp E "--speed requires a numeric value."
|
||||
_rain_show_usage
|
||||
return 1
|
||||
}
|
||||
shift
|
||||
else
|
||||
disp E "--speed requires a numeric value."
|
||||
_rain_show_usage
|
||||
return 1
|
||||
fi
|
||||
;;
|
||||
-c|--color)
|
||||
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
|
||||
base_color="$2"
|
||||
shift
|
||||
else
|
||||
disp E "--color requires a color name."
|
||||
_rain_show_usage
|
||||
return 1
|
||||
fi
|
||||
;;
|
||||
-h|--help)
|
||||
_rain_show_usage
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Unknown option: $1"
|
||||
_rain_show_usage
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
_rain_engine "$step_duration" "$base_color" "rain" ""
|
||||
}
|
||||
export -f rain
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Matrix style digital rain
|
||||
# Usage: matrix [OPTIONS]
|
||||
matrix()
|
||||
{
|
||||
_matrix_show_usage()
|
||||
{
|
||||
printf "Usage: matrix [OPTIONS]\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-s, --speed NUM Set speed value (default: 3.5 => 0.035s).\n"
|
||||
printf "\t Values >=1 use a /100 scale (3.5 => 0.035s).\n"
|
||||
printf "\t Values <1 are interpreted as raw seconds.\n"
|
||||
printf "\t-c, --color COLOR Set color theme (default: green).\n"
|
||||
printf "\t-C, --charset SET Character set: binary, kana, ascii (default: binary).\n"
|
||||
printf "\t-h, --help Display this help message and exit.\n\n"
|
||||
printf "Example: matrix -C kana -c green --speed 2\n"
|
||||
}
|
||||
|
||||
local step_duration=0.035
|
||||
local base_color="green"
|
||||
local charset="binary"
|
||||
|
||||
while [[ "$#" -gt 0 ]]; do
|
||||
case $1 in
|
||||
-s|--speed)
|
||||
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
|
||||
step_duration=$(_rain_normalize_speed "$2") || {
|
||||
disp E "--speed requires a numeric value."
|
||||
_matrix_show_usage
|
||||
return 1
|
||||
}
|
||||
shift
|
||||
else
|
||||
disp E "--speed requires a numeric value."
|
||||
_matrix_show_usage
|
||||
return 1
|
||||
fi
|
||||
;;
|
||||
-c|--color)
|
||||
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
|
||||
case "${2,,}" in
|
||||
binary|kana|kanji|ascii)
|
||||
disp W "'${2}' looks like a charset value. Use -C/--charset for clarity."
|
||||
charset="${2,,}"
|
||||
;;
|
||||
*)
|
||||
base_color="$2"
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
else
|
||||
disp E "--color requires a color name."
|
||||
_matrix_show_usage
|
||||
return 1
|
||||
fi
|
||||
;;
|
||||
-C|--charset)
|
||||
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
|
||||
charset="${2,,}"
|
||||
shift
|
||||
else
|
||||
disp E "--charset requires a value."
|
||||
_matrix_show_usage
|
||||
return 1
|
||||
fi
|
||||
;;
|
||||
-h|--help)
|
||||
_matrix_show_usage
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Unknown option: $1"
|
||||
_matrix_show_usage
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
_rain_engine "$step_duration" "$base_color" "matrix" "$charset"
|
||||
}
|
||||
export -f matrix
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -36,17 +36,42 @@
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Remove host from know_host (name and IP) for the active user
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: rmhost <hostname|ip> [hostname2|ip2 [...]]
|
||||
rmhost()
|
||||
{
|
||||
if [[ "$#" -lt 1 ]]; then
|
||||
disp E "Incorrect number of parameters."
|
||||
disp E "Usage: rmhost <hostname|ip> [hostname2|ip2 [...]]"
|
||||
local PARSED
|
||||
PARSED=$(getopt -o h --long help -n 'rmhost' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then return 1; fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "rmhost: Remove host/IP from ~/.ssh/known_hosts.\n\n"
|
||||
printf "Usage: rmhost <hostname|ip> [hostname2|ip2 ...]\n\n"
|
||||
printf "Options:\n"
|
||||
printf " -h, --help Display this help screen\n"
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid options, use \"rmhost --help\" to display usage."
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Validation: Ensure at least one argument remains
|
||||
if [[ $# -eq 0 ]]; then
|
||||
disp E "Missing argument. Use 'rmhost --help' for usage."
|
||||
return 1
|
||||
fi
|
||||
|
||||
while [[ $1 ]]; do
|
||||
local hst=$1 && shift
|
||||
for target in "$@"; do
|
||||
local hst=$target
|
||||
isipv4 "$hst" >/dev/null
|
||||
local v4=$?
|
||||
isipv6 "$hst" >/dev/null
|
||||
@@ -81,20 +106,36 @@ rmhost()
|
||||
done
|
||||
}
|
||||
export -f rmhost
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Login root via SSH on the given machine
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: ssr <server [ssh options]>
|
||||
ssr()
|
||||
{
|
||||
for opt in $@; do
|
||||
case $opt in
|
||||
"-h" | "--help")
|
||||
echo "ssr: do a root user ssh login."
|
||||
echo
|
||||
echo "Usage: ssr <server [ssh options]>"
|
||||
local PARSED
|
||||
PARSED=$(getopt -o h --long help -n 'ssr' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then return 1; fi
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "ssr: SSH into a server as root.\n\n"
|
||||
printf "Usage: ssr <server> [ssh_options...]\n\n"
|
||||
printf "Options:\n"
|
||||
printf "\t-h, --help\t\tDisplay this help screen\n"
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid options, use \"ssr --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
@@ -102,15 +143,17 @@ ssr()
|
||||
disp E "ssh is not installed."
|
||||
return 127
|
||||
}
|
||||
[[ ! $1 ]] &&
|
||||
disp E "Please specify the server you want to log in." &&
|
||||
[[ ! $1 ]] && {
|
||||
disp E "Please specify the server you want to log in."
|
||||
return 1
|
||||
}
|
||||
|
||||
local srv=$1 && shift
|
||||
|
||||
ssh -Y root@"$srv" "$@"
|
||||
}
|
||||
export -f ssr
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -40,14 +40,40 @@ export ARCH_URL="$BASE_URL/archive/master.tar.gz"
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Check for profile updates
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: check_updates [-q]
|
||||
# If -q is specified, the function will operate in quiet mode (internal use only)
|
||||
check_updates()
|
||||
{
|
||||
if [[ $1 == "-q" ]]; then
|
||||
# Quiet mode is mostly used internally when profile_upgrade is called
|
||||
quiet=1
|
||||
local quiet=0
|
||||
local PARSED=$(getopt -o hq --long help,quiet -n 'check_updates' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"check_updates --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
[[ -n $quiet ]] && disp I "Checking for updates..."
|
||||
eval set -- "$PARSED"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "check_updates: Check for new versions.\n\n"
|
||||
printf "Usage: check_updates\n"
|
||||
return 0
|
||||
;;
|
||||
-q|--quiet)
|
||||
quiet=1
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
(( $quiet != 1 )) && disp I "Checking for updates..."
|
||||
local vfile="/tmp/version"
|
||||
wget "$UPDT_URL/version" -O $vfile >/dev/null 2>&1 || {
|
||||
disp E "Can't download version file, impossible to proceed!"
|
||||
@@ -58,10 +84,10 @@ check_updates()
|
||||
local lastver=$(cat $vfile)
|
||||
if [[ $lastver != $PROFVERSION ]]; then
|
||||
disp I "You have version $PROFVERSION installed. Version $lastver is available."
|
||||
[[ $quiet ]] && disp I "You should upgrade to last version when possible."
|
||||
(( $quiet != 1 )) && disp I "You should upgrade to last version when possible."
|
||||
result=1
|
||||
else
|
||||
[[ -n $quiet ]] && disp I "Your version is up-to-date."
|
||||
(( $quiet != 1 )) && disp I "Your version is up-to-date."
|
||||
result=0
|
||||
fi
|
||||
rm -f $vfile
|
||||
@@ -71,12 +97,39 @@ check_updates()
|
||||
unset lastver vfile
|
||||
return $result
|
||||
}
|
||||
export -f check_updates
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Apply update to profile
|
||||
# ------------------------------------------------------------------------------
|
||||
# Usage: profile_upgrade
|
||||
profile_upgrade()
|
||||
{
|
||||
local PARSED=$(getopt -o h --long help -n 'profile_upgrade' -- "$@")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
printf "Invalid options, use \"profile_upgrade --help\" to display usage."
|
||||
return 1
|
||||
fi
|
||||
eval set -- "$PARSED"
|
||||
while true; do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
printf "profile_upgrade: Upgrade the profile to the latest version.\n\n"
|
||||
printf "Usage: profile_upgrade\n"
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Invalid options, use \"profile_upgrade --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if check_updates -q; then
|
||||
disp "No update available."
|
||||
return 0
|
||||
@@ -89,15 +142,17 @@ profile_upgrade()
|
||||
|
||||
if [[ -d $MYPATH/.git ]]; then
|
||||
disp I "Git installation detected, applying git pull."
|
||||
local curdir=$(pwd)
|
||||
cd $MYPATH
|
||||
pushd "$MYPATH" || {
|
||||
disp E "Failed to change directory to $MYPATH."
|
||||
return 3
|
||||
}
|
||||
git pull || {
|
||||
disp E "Git pull failed, upgrade not applyed."
|
||||
cd "$curdir"
|
||||
popd
|
||||
return 2
|
||||
}
|
||||
disp I "Successfully upgraded using git."
|
||||
cd "$curdir"
|
||||
popd
|
||||
else
|
||||
disp I "No Git detected. Downloading and applying upgrade from archive..."
|
||||
local tmpdir="/tmp/profile_upg.$$"
|
||||
@@ -130,4 +185,8 @@ profile_upgrade()
|
||||
rm -rf "$tmpdir"
|
||||
fi
|
||||
}
|
||||
export -f profile_upgrade
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
188
profile.sh
188
profile.sh
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
# Begin profile
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||
# Protected by the BSD3 license. Please read bellow for details.
|
||||
#
|
||||
# * Redistribution and use in source and binary forms,
|
||||
@@ -35,15 +35,21 @@
|
||||
# * OF SUCH DAMAGE.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
if [[ ! $SHELL =~ bash|zsh ]]; then
|
||||
echo "That environment script is designed to be used with bash or zsh being the shell."
|
||||
echo "Please consider using bash or zsh instead, or patch me ;)!"
|
||||
if [[ ! $SHELL =~ bash ]]; then
|
||||
echo "That environment script is designed to be used with bash being the shell."
|
||||
echo "Please consider using bash to enjoy our features!"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Required for associative arrays (4.0+) and namerefs (4.3+)
|
||||
if ((BASH_VERSINFO[0] < 4)) || [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 3 ]]; then
|
||||
echo "[ Error ] This profile requires Bash 4.3 or higher."
|
||||
echo "Current version: $BASH_VERSION"
|
||||
return 1 2>/dev/null || exit 1
|
||||
fi
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# path* : private functions for PATH variable management
|
||||
# ------------------------------------------------------------------------------
|
||||
pathremove()
|
||||
{
|
||||
local IFS=':'
|
||||
@@ -71,6 +77,110 @@ pathappend()
|
||||
local pathvar=${2:-PATH}
|
||||
export $pathvar="${!pathvar:+${!pathvar}:}$1"
|
||||
}
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Configuration file parser
|
||||
parse_conf()
|
||||
{
|
||||
local config_file="$1"
|
||||
local current_section=""
|
||||
local line key value
|
||||
|
||||
[[ ! -f "$config_file" ]] && return 1
|
||||
|
||||
while IFS='=' read -r key value || [[ -n "$key" ]]; do
|
||||
# Internal trimming (removes leading/trailing whitespace & CR)
|
||||
key="${key%"${key##*[![:space:]]}"}"
|
||||
key="${key#"${key%%[![:space:]]*}"}"
|
||||
key="${key%$'\r'}" # Strip potential Windows line endings
|
||||
|
||||
# Skip comments and empty lines
|
||||
[[ -z "$key" || "$key" =~ ^[#\;] ]] && continue
|
||||
|
||||
# Section Detection: [section_name]
|
||||
if [[ "$key" =~ ^\[([a-zA-Z0-9_]+)\]$ ]]; then
|
||||
current_section="${BASH_REMATCH[1]}"
|
||||
declare -g -A "CONF_$current_section"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Secure Assignment (if inside a section)
|
||||
if [[ -n "$current_section" ]]; then
|
||||
# Clean the value
|
||||
value="${value%"${value##*[![:space:]]}"}"
|
||||
value="${value#"${value%%[![:space:]]*}"}"
|
||||
value="${value%$'\r'}"
|
||||
|
||||
# Protect against command injection by disallowing certain characters in keys
|
||||
value="${value//\`/}"
|
||||
value="${value//\$\(/}"
|
||||
|
||||
# Correctly interpretet internal variables (e.g. $HOME)
|
||||
if [[ "$value" == *\$* ]]; then
|
||||
value=$(envsubst <<< "$value")
|
||||
fi
|
||||
|
||||
# Strip quotes (handling both " and ')
|
||||
value="${value%\"}"; value="${value#\"}"
|
||||
value="${value%\'}"; value="${value#\'}"
|
||||
|
||||
# Use a nameref for safe, eval-free assignment
|
||||
local -n current_array="CONF_$current_section"
|
||||
current_array["$key"]="$value"
|
||||
fi
|
||||
done < "$config_file"
|
||||
}
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Load command aliases from configuration
|
||||
load_alias()
|
||||
{
|
||||
local section_name="CONF_$1"
|
||||
|
||||
# Check if the associative array exists using declare -p
|
||||
[[ "$(declare -p "$section_name" 2>/dev/null)" != "declare -A"* ]] && return 1
|
||||
|
||||
# Create a nameref to the section array
|
||||
local -n current_aliases="$section_name"
|
||||
|
||||
# Iterate safely over the keys of the associative array
|
||||
for key in "${!current_aliases[@]}"; do
|
||||
local cmd="${current_aliases[$key]}"
|
||||
|
||||
# Extract the base command (first word) safely without awk
|
||||
local base_cmd="${cmd%% *}"
|
||||
|
||||
# Only alias if the base command is executable
|
||||
if command -v "$base_cmd" >/dev/null 2>&1; then
|
||||
alias "$key"="$cmd"
|
||||
fi
|
||||
done
|
||||
}
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Load configuration values as environment variables
|
||||
load_conf()
|
||||
{
|
||||
local section_name="CONF_$1"
|
||||
|
||||
[[ "$(declare -p "$section_name" 2>/dev/null)" != "declare -A"* ]] && return 1
|
||||
|
||||
local -n current_vars="$section_name"
|
||||
|
||||
for key in "${!current_vars[@]}"; do
|
||||
# Export the key/value pair as a standard shell variable
|
||||
# We use 'export' directly; Bash handles the assignment safely here
|
||||
export "$key"="${current_vars[$key]}"
|
||||
done
|
||||
}
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# ------------------------------------------------------------------------------
|
||||
@@ -79,8 +189,10 @@ pathappend()
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# Store script's path (realpath -s resolve symlinks if profile.sh is a symlink)
|
||||
# Because we're more likely to be sourced, we use BASH_SOURCE to get the path
|
||||
# of the sourced file instead of $0
|
||||
if [[ -z "$PROFILE_PATH" ]]; then
|
||||
export MYPATH=$(dirname "$(realpath -s "$0")")
|
||||
export MYPATH=$(dirname "$(realpath -s "${BASH_SOURCE[0]}")")
|
||||
else
|
||||
export MYPATH="$PROFILE_PATH"
|
||||
fi
|
||||
@@ -101,41 +213,14 @@ export PROFVERSION=$(cat "$MYPATH"/version)
|
||||
if [[ $EUID -eq 0 ]]; then
|
||||
pathappend /sbin:/usr/sbin
|
||||
fi
|
||||
[[ -d /share/services/gestparc ]] && pathappend /share/services/gestparc
|
||||
[[ -d ~/bin ]] && pathappend ~/bin
|
||||
[[ -d ~/.local/bin ]] && pathappend ~/.local/bin
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Default values are set here and will be overloaded with config file if any
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# Set bash history
|
||||
export HISTSIZE=50000
|
||||
export HISTIGNORE="&:[bf]g:exit"
|
||||
|
||||
# Set default pager
|
||||
export PAGER=less
|
||||
|
||||
# More colors
|
||||
export TERM=xterm-256color
|
||||
|
||||
# Set some compiling values
|
||||
export CFLAGS="-O2 -pipe -march=native"
|
||||
export MAKEFLAGS='-j12'
|
||||
export PKGSOURCES='/share/src/archives'
|
||||
|
||||
# Default city for weather forcast
|
||||
export DEFAULT_CITY="Toulouse"
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Default values could be altered after this line
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# Load global configuration
|
||||
[[ -f $MYPATH/etc/profile.conf ]] && . $MYPATH/etc/profile.conf
|
||||
|
||||
# Load personal configuration
|
||||
[[ -f ~/.profile.conf ]] && . ~/.profile.conf
|
||||
# Parse and load general configuration
|
||||
export PROFILE_CONF="$MYPATH/profile.conf"
|
||||
parse_conf "$PROFILE_CONF"
|
||||
load_conf system # Load Bash system behavior configuration (history, pager, etc.)
|
||||
load_conf general # General purpose configuration (compilation flags, etc.)
|
||||
|
||||
# Load module scripts
|
||||
for script in $MYPATH/profile.d/*.sh; do
|
||||
@@ -152,30 +237,7 @@ done
|
||||
if [[ $INTERACTIVE ]]; then
|
||||
# For compiling (as we often compile with LFS/0linux...)
|
||||
#Aliases
|
||||
alias ll='ls -laFh --color=auto'
|
||||
alias la='ls -Ah --color=auto'
|
||||
alias l='ls -CF --color=auto'
|
||||
alias ls='ls --color=auto'
|
||||
|
||||
alias grep='grep --color=auto'
|
||||
alias egrep='egrep --color=auto'
|
||||
alias fgrep='fgrep --color=auto'
|
||||
alias qfind="find . -name "
|
||||
|
||||
alias mkck='make check'
|
||||
alias mkin='make install'
|
||||
alias mkdin='make DESTDIR=$PWD/dest-install install'
|
||||
alias ssh='ssh -Y'
|
||||
|
||||
alias wget='wget -c' # resume mode by default
|
||||
alias myip='curl ip.appspot.com'
|
||||
|
||||
# Human readable by default
|
||||
alias df='df -H'
|
||||
alias du='du -ch'
|
||||
|
||||
alias sdu='du -sk ./* | sort -n'
|
||||
alias hdu='du -hs ./* | sort -H'
|
||||
load_alias aliases
|
||||
|
||||
# Define PS1
|
||||
trap 'timer_start' DEBUG
|
||||
@@ -183,7 +245,7 @@ if [[ $INTERACTIVE ]]; then
|
||||
|
||||
# Set default language
|
||||
setfr
|
||||
showinfo
|
||||
showinfo && printf "\n"
|
||||
check_updates -q
|
||||
disp I "Profile version $PROFVERSION chargé..."
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user