This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Util.sh

129 lines
2.8 KiB
Bash
Raw Normal View History

2023-11-23 00:13:51 +03:00
# Get the number of avaialble cores (threads).
2023-12-07 01:44:42 +03:00
function _core_count() {
2023-12-05 21:50:45 +03:00
cat /proc/cpuinfo | grep ^processor | wc -l
2023-11-23 00:13:51 +03:00
}
# Get the number of available memory (in mebibytes).
function _mem_free() {
free -m | sed -n -e '2p' | awk '{print $7}'
}
2023-12-16 21:56:30 +03:00
# Function-wrapper to iterate with specified function with provided files.
# By default Iterates on all non-hidden files and directories.
2023-12-17 20:12:33 +03:00
# List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, skipped - count of skipped files, status - status line (not recommended to use).
2023-12-16 21:56:30 +03:00
# Usage: _iterate_targets <FUNCTION> [FILES]
function _iterate_targets() {
local IFS=$'\n'
local foo="${1}"
local targets=("${@:2}")
local total=${#targets[@]}
local count=0
local failed=0
2023-12-17 20:12:33 +03:00
local skipped=0
2023-12-17 16:27:21 +03:00
local code=0
2023-12-16 21:56:30 +03:00
# set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then
2023-12-17 16:27:21 +03:00
_error "No targets provided."
return 1
2023-12-16 21:56:30 +03:00
fi
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# status info.
2023-12-16 22:52:36 +03:00
local status="[${count}/${total}] ${target}"
_info "${status}"
2023-12-16 21:56:30 +03:00
# Call function.
${foo} "${target}"
# Show error.
if [[ ${?} != 0 ]]; then
((failed++))
_error "${status}: Failed."
2023-12-16 21:56:30 +03:00
fi
2023-12-17 16:27:21 +03:00
# Add newline if not the last one.
[[ "${count}" = "${total}" ]] || echo
2023-12-16 21:56:30 +03:00
done
2023-12-17 20:12:33 +03:00
# Show skipped.
if [[ ${skipped} != 0 ]]; then
echo
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
fi
2023-12-16 21:56:30 +03:00
# Show error.
if [[ ${failed} != 0 ]]; then
2023-12-17 20:12:33 +03:00
[[ "${skipped}" = 0 ]] && echo
2023-12-16 21:56:30 +03:00
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
2023-12-17 16:27:21 +03:00
2024-01-03 18:50:59 +03:00
# Skip current iteration.
# Usage: _iterate_skip [MESSAGE]
function _iterate_skip() {
((skipped++))
[[ "${*}" = "" ]] || echo -e "${color_byellow}${*}${color_default}"
}
# Report an error.
2023-12-17 16:27:21 +03:00
# Always returns code 1.
2023-12-17 20:12:33 +03:00
# Usage: _error <MESSAGE>
2023-12-17 16:27:21 +03:00
function _error() {
2024-01-03 18:50:59 +03:00
>&2 echo -e "${color_bred}${*}${color_default}"
2023-12-17 20:12:33 +03:00
return 1
}
2024-01-03 18:50:59 +03:00
# Report a warning.
2023-12-17 20:12:33 +03:00
# Usage: _warn <MESSAGE>
function _warn() {
2024-01-03 18:50:59 +03:00
>&2 echo -e "${color_byellow}${*}${color_default}"
2023-12-17 16:27:21 +03:00
}
2023-12-17 20:44:54 +03:00
2024-01-03 18:50:59 +03:00
# Report a debug.
2023-12-21 22:51:38 +03:00
# Usage: _debug <MESSAGE>
function _debug() {
2024-01-03 18:50:59 +03:00
>&2 echo -e "${color_bwhite}${*}${color_default}"
2023-12-21 22:51:38 +03:00
}
2024-01-03 18:50:59 +03:00
# Report an info.
2023-12-23 17:41:20 +03:00
# Usage: _info <MESSAGE>
function _info() {
2024-01-03 18:50:59 +03:00
>&2 echo -e "${color_bwhite}${*}${color_default}"
2023-12-17 20:44:54 +03:00
}
2024-01-09 19:53:36 +03:00
# Check if array contains an element (strict).
# Usage: _contains <ELEMENT> <ARRAY>
function _contains() {
local IFS=$'\n'
local target="${1}"
local array="${@:2}"
if [[ "${target}" = "" ]] || [[ "${array}" = "" ]]; then
help _contains
return 2
fi
for item in ${array[@]}; do
[[ "${item}" = "${target}" ]] && return 0
done
return 1
}
2024-01-23 15:57:39 +03:00
# Check if inside Tmux.
function _is_tmux() {
[[ "${TERM_PROGRAM}" = "tmux" ]]
}
2024-01-28 21:27:27 +03:00
# Check if root.
function _is_root() {
[[ "${UID}" = 0 ]]
}