]> git.armaanb.net Git - bin.git/commitdiff
fortune: add script
authorArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 02:20:51 +0000 (21:20 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 02:20:51 +0000 (21:20 -0500)
Simple fortune implementation in awk. Still needs argument parsing.

fortune [new file with mode: 0755]

diff --git a/fortune b/fortune
new file mode 100755 (executable)
index 0000000..02276e6
--- /dev/null
+++ b/fortune
@@ -0,0 +1,22 @@
+#!/usr/bin/awk -f
+# Fortune implementation in awk
+
+BEGIN {
+  "date +%N" | getline time
+  srand(time)
+  "grep '%' " ARGV[1] " | wc -l" | getline n
+  line = int(rand() * n)
+}
+
+/%/ {
+  if (p) {
+    exit
+  }
+  z++
+  next
+}
+
+z == line {
+  print $0
+  p = 1
+}