]> git.armaanb.net Git - bin.git/commitdiff
fortune: fix bug
authorArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 14:19:20 +0000 (09:19 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 14:19:20 +0000 (09:19 -0500)
Fix small error where sometimes it return blank, and never return the
last item.

fortune

diff --git a/fortune b/fortune
index 3c8529425df9112d94f5657a46b12a8f89fe6f15..8d1ed43f441b63d798502811a19b1c7277982666 100755 (executable)
--- a/fortune
+++ b/fortune
@@ -39,17 +39,22 @@ while [ "$1" != "" ]; do
   shift
 done
 
-awk '$1 == "%" {
-  sub(/%/, "")
-  ++f
+awk '
+BEGIN {
+  f++
+}
+
+$1 == "%" {
+  f++
 }
 
 {
+  sub("%", "")
   fortunes[f] = fortunes[f] (length(fortunes[f])?"\n":"") $0
 }
 
 END {
   "date +%N" | getline time
   srand(time)
-  print fortunes[int(rand() * f) + 1]
+  print fortunes[int(rand() * f-2) + 2]
 }' $FILE