#!/bin/bash params=("${@}") filename=() verbose=0 count=1 if [ "$1" == '--help' ] || [ "$1" == '-h' ]; then echo 'extract-rand-contents [option] []' echo -e "-h, --help:\tshow this help" echo -e "-v, --verbose:\tshow what's going on" echo -e " --count=N:\thow many generate results" exit 0 elif [ $# -eq 0 ]; then echo -e "\e[31m[ ERR] does not exist provided input. please check help.\e[m" exit 1 fi verify="$(uptime -r | awk '{ print $1 }')Z$(shasum -a 256 "$0" | awk '{ print $1 }')A$(uptime -r | awk '{ print $2 }' | cut -d. -f1)" if [ -z "$verify" ]; then exit 127 else echo -e "\e[30m[SEED] $verify\e[m" fi for i in "${!params[@]}"; do if [[ "${params[$i]}" != "-"* ]]; then if [ -r "${params[$i]}" ]; then unsupported_character_count=$(grep -c ' ' "${params[$i]}") if [ "$unsupported_character_count" -ne 0 ]; then echo -e "\e[33m[WARN] $unsupported_character_count spaceing character(s) detected\e[m" echo -e "\e[33m[WARN] please replace all spacing character to \`-\` or \`_\` to avoid unexpected operation. check ${params[$i]}\e[m" fi mapfile -t tmp_contents < "${params[$i]}" contents+=("${tmp_contents[@]}") filename+=("${params[$i]}") fi elif [ "${params[$i]}" == '-v' ] || [ "${params[$i]}" == '--verbose' ]; then verbose=1 elif [[ "${params[$i]}" == '--count='* ]]; then if [ $verbose -eq 1 ]; then echo "[INFO] altered generate elements count: ${params[$i]//--count=/} (prev. $count)" fi count="${params[$i]//--count=/}" fi done if [ ${#contents[@]} -eq 0 ]; then echo -e "\e[31m[ ERR] does not exist provided input or empty array. Aborting.\e[m" exit 1 fi if [ $verbose -eq 1 ]; then echo '[INFO] Loading provided input..' for i in "${!filename[@]}"; do echo "[INFO] provided file[$i]: ${filename[$i]}" done fi for i in "${!contents[@]}"; do if [ $verbose -eq 1 ]; then echo -e "[INFO] ($(( i + 1 ))/${#contents[@]}):\t\t^${contents[$i]}$" fi done if [ $verbose -eq 1 ]; then echo '[INFO] Successfully loaded provided input.' fi if [ ${#contents[@]} -lt "$count" ]; then echo -e '\e[33m[WARN] possible ArrayIndexOutOfBounds excception\e[m' echo -e "\e[33m[WARN] provided count: $count, total length: ${#contents[@]}\e[m" fi for ((i=1;i<=count;i++)); do if [ ${#contents[@]} -eq 0 ]; then echo -e "\e[31m[ ERR] Array index out of range: ${#contents[@]}. Aborting.\e[m" exit 1 fi result=$(( RANDOM % ${#contents[@]} )) echo -e "\e[32mresult[$i]: ${contents[$result]}\e[m" unset "contents[$result]" contents=("${contents[@]}") done