Created
April 12, 2022 06:40
-
-
Save takakabe/8d7af32dd7d8fa7edd450a06aab50a2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage() { | |
echo 'usage: ./graph.sh --file [PathToFile]' | |
} | |
while [[ ${#} -gt 0 ]]; do | |
opt="${1}" | |
shift | |
value="${1}" | |
if [[ "${value}" =~ ^-{1,2}.* ]]; then | |
echo "Error: Argument is blank." | |
exit 1 | |
fi | |
case "${opt}" in | |
"-f"|"--file" ) | |
file="${1}" | |
shift | |
;; | |
* ) | |
echo "ERROR: Invalid option: \""${opt}"\"" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
if [[ -z "${file}" ]]; then | |
usage | |
exit 1 | |
fi | |
max_value=`cat ${file} | awk '{print $2}' | sort -nr | head -n 1` | |
min_value=`cat ${file} | awk '{print $2}' | sort -n | head -n 1` | |
one_block=$((max_value / min_value)) | |
max_length_item=`cat ${file} | awk '{print length($1)}' | sort -nr | head -n 1` | |
while read line; do | |
item=`echo ${line} | awk '{print $1}'` | |
value=`echo ${line} | awk '{print $2}'` | |
block_sum=$((value / min_value)) | |
printf "%-${max_length_item}s%-2s" "${item}" | |
for block in `seq ${block_sum}`; do | |
echo -n "▇" | |
done | |
echo " ${value}" | |
done < ${file} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment