# 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. # List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, status - status line (not recommended to use). # Usage: _iterate_targets [FILES] function _iterate_targets() { local IFS=$'\n' local foo="${1}" local targets=("${@:2}") local total=${#targets[@]} local count=0 local failed=0 local code=0 # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then _error "No targets provided." return 1 fi # iterate each target. for target in "${targets[@]}"; do # increment counter. ((count++)) # status info. local status="[${count}/${total}] ${target}" 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 # Add newline if not the last one. [[ "${count}" = "${total}" ]] || echo done # Show error. if [[ ${failed} != 0 ]]; then echo echo -e "${color_bred}Failed: ${failed}.${color_default}" false fi } # Report error to stdout. # Always returns code 1. # If no message specified, prints "An error has occured". # Usage: _error [MESSAGE] function _error() { echo -e "${color_bred}${*}${color_default}" }