File: def.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2024 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the “Software”), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # def [words...] 27 # 28 # DEFine/lookup words via the online english dictionary from https://dict.org 29 30 31 if [ $# -eq 0 ]; then 32 # printf "\e[31mno words to lookup given\e[0m\n" > /dev/stderr 33 # exit 1 34 35 awk '/^# +def/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 36 exit 0 37 fi 38 39 # handle leading options 40 case "$1" in 41 -h|--h|-help|--help) 42 awk '/^# +def/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 43 exit 0 44 ;; 45 esac 46 47 # keep track of this script's eventual success/failure 48 errcode=0 49 50 for word in "$@"; do 51 printf "\n\e[7m%-80s\e[0m\n" "${word}" 52 53 curl -s "dict://dict.org/d:${word}" | sed 's-\r--g' | awk ' 54 # get rid of carriage-returns at the end of lines 55 { gsub(/\r/, "") } 56 57 # code 151 has the specific source for the match, so 58 # use a unique style for that kind of metadata line 59 /^151 / { 60 printf "\x1b[38;5;4m%s\x1b[0m\n", $0 61 next 62 } 63 64 # make error-type metadata lines stand out 65 /^[3-5][0-9]{2} [a-z]/ { 66 nerr++ 67 printf "\x1b[38;5;124m%s\x1b[0m\n", $0 68 next 69 } 70 71 # gray out ok-type metadata lines 72 /^[12][0-9]{2} / || /^\.$/ { 73 printf "\x1b[38;5;244m%s\x1b[0m\n", $0 74 next 75 } 76 77 # keep all other lines unstyled 78 1 79 80 # fail the script if any lookup failed 81 END { if (nerr > 0) exit 1 }' 82 83 # any lookup failure eventually results in the whole script failing, 84 # but only after trying to show results for all words given 85 if [ $? -ne 0 ]; then 86 errcode=1 87 fi 88 done 89 90 exit "${errcode}"