From 0e32ab680489497de6b08f3c0dd617caea55a820 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Sun, 28 Feb 2021 23:00:41 -0500 Subject: [PATCH] fortune: Wrap in shell script and remove grep call Thanks to e36freak on #awk --- fortune | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/fortune b/fortune index 02276e6..3f4e961 100755 --- 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"} -- 2.39.2