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

57 lines
1.3 KiB
Bash

# Get the number of avaialble cores (threads).
function _core_count() {
cat /proc/cpuinfo | grep ^processor | wc -l
}
# Parse integers from mixed string.
function _parse_ints() {
echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g'
}
# Function-wrapper to iterate with specified function with provided files.
# By default Iterates on all non-hidden files and directories.
# 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
# set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then
targets=($(ls))
total=${#targets[@]}
fi
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# status info.
if [[ "${total}" -gt 1 ]]; then
local status="[${count}/${total}] ${target}"
else
local status="${target}"
fi
echo -e "${color_bwhite}${status}${color_default}"
# Call function.
${foo} "${target}"
# Show error.
if [[ ${?} != 0 ]]; then
((failed++))
echo -e "${color_bred}${status}: Failed.${color_default}"
fi
done
# Show error.
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}