]> git.armaanb.net Git - bin.git/commitdiff
fortune: Wrap in shell script and remove grep call
authorArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 04:00:41 +0000 (23:00 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 04:00:41 +0000 (23:00 -0500)
Thanks to e36freak on #awk

fortune

diff --git a/fortune b/fortune
index 02276e6ecf76e4b612583e6a397668312494d1c2..3f4e961f2b980de37683431b54dad3057b1f6873 100755 (executable)
--- a/fortune
+++ b/fortune
@@ -1,22 +1,17 @@
-#!/usr/bin/awk -f
+#!/usr/bin/sh -e
 # Fortune implementation in awk
 
-BEGIN {
-  "date +%N" | getline time
-  srand(time)
-  "grep '%' " ARGV[1] " | wc -l" | getline n
-  line = int(rand() * n)
+awk '$1 == "%" {
+  sub(/%/, "")
+  ++f
 }
 
-/%/ {
-  if (p) {
-    exit
-  }
-  z++
-  next
+{
+  fortunes[f] = fortunes[f] (length(fortunes[f])?"\n":"") $0
 }
 
-z == line {
-  print $0
-  p = 1
-}
+END {
+  "date +%N" | getline time
+  srand(time)
+  print fortunes[int(rand() * f) + 1]
+}' ${1:-"/usr/share/fortune/cookie"}