diff --git a/top_words.sh b/top_words.sh index 74d811d..90a7e07 100755 --- a/top_words.sh +++ b/top_words.sh @@ -3,22 +3,29 @@ set -o errexit set -o nounset set -o pipefail -url="${1:-}" -basedir=$(dirname "$0") +fetch_top_words() { + local url + local basedir + + url="${1:-}" + basedir=$(dirname "$0") + -if [[ "${url:-}" = "" ]]; then - grep --only-matching --extended-regexp '[a-zA-Z]{3,}' "$basedir/README.md" \ - | tr '[:upper:]' '[:lower:]' \ - | grep --invert-match --word-regexp --fixed-strings --file=stopwords.txt \ - | sort \ - | uniq --count \ - | sort -n -else - curl "$url" \ - | grep --only-matching --extended-regexp '[a-zA-Z]{3,}' \ - | tr '[:upper:]' '[:lower:]' \ - | grep --invert-match --word-regexp --fixed-strings --file=stopwords.txt \ - | sort \ - | uniq --count \ - | sort -n -fi + if [[ "${url:-}" = "" ]]; then + grep --only-matching --extended-regexp '[a-zA-Z]{3,}' "$basedir/README.md" \ + | tr '[:upper:]' '[:lower:]' \ + | grep --invert-match --word-regexp --fixed-strings --file=stopwords.txt \ + | sort \ + | uniq --count \ + | sort -n + else + curl "$url" \ + | grep --only-matching --extended-regexp '[a-zA-Z]{3,}' \ + | tr '[:upper:]' '[:lower:]' \ + | grep --invert-match --word-regexp --fixed-strings --file=stopwords.txt \ + | sort \ + | uniq --count \ + | sort -n + fi +} +fetch_top_words "$@"