File: coy.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 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 # coy [exponent]
  27 # coy [exponent] [years]
  28 # coy [amount] [exponent] [years]
  29 #
  30 # Compound Over Years does what it says, by showing a TSV table. It accepts
  31 # 1, 2, or 3 arguments for the parameters: the default amount is 1.0, while
  32 # the default year-span is 10.
  33 
  34 
  35 case "$1" in
  36     -h|--h|-help|--help)
  37         awk '/^# +coy /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  38         exit 0
  39     ;;
  40 esac
  41 
  42 awk '
  43 BEGIN {
  44     quantity = 1.0
  45     exponent = 1.0
  46     years = 10
  47 
  48     switch (ARGC) {
  49         case 2:
  50             exponent = ARGV[1] + 0.0
  51             break;
  52 
  53         case 3:
  54             exponent = ARGV[1] + 0.0
  55             years = ARGV[2] + 0
  56             break;
  57 
  58         case 4:
  59             quantity = ARGV[1] + 0.0
  60             exponent = ARGV[2] + 0.0
  61             years = ARGV[3] + 0
  62             break;
  63 
  64         default:
  65             msg = "\x1b[31monly 1, 2, or 3 arguments are supported\x1b[0m\n"
  66             print msg > "/dev/stderr"
  67             exit 1
  68     }
  69 
  70     print "year\tquantity"
  71     for (k = 1.0; y <= years; y++) {
  72         printf "%d\t%.4f\n", y, quantity * k
  73         k *= exponent
  74     }
  75     exit
  76 }
  77 ' "$@"