aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-004/abigail/README.md1
-rw-r--r--challenge-004/abigail/bash/ch-2.sh37
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-004/abigail/README.md b/challenge-004/abigail/README.md
index 951045328c..be970d21a6 100644
--- a/challenge-004/abigail/README.md
+++ b/challenge-004/abigail/README.md
@@ -46,4 +46,5 @@ The sets of letters are read from standard input.
### Solutions
* [GNU AWK](awk/ch-2.gawk)
+* [Bash](bash/ch-2.sh)
* [Perl](perl/ch-2.pl)
diff --git a/challenge-004/abigail/bash/ch-2.sh b/challenge-004/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..e9a2935272
--- /dev/null
+++ b/challenge-004/abigail/bash/ch-2.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-2.sh -f FILE < input-file
+#
+
+#
+# Disable pathname expansion
+#
+set -f
+
+#
+# Read the option
+#
+while getopts "f:" name
+do if [ "$name" = "f" ]
+ then file=$OPTARG
+ fi
+done
+
+
+while read letters
+do letters=${letters^^[a-z]} # Upper case the letters
+ while read word # Read word from file
+ do copy=${word^^[a-z]} # Make a copy, and upper case it
+ for ((i = 0; i < ${#letters}; i ++)) # Iterate over letters
+ do copy=${copy/${letters:$i:1}/} # Remove letter from copy
+ done
+ if [ ${#copy} -eq 0 ] # If we end up with an empty
+ then echo $word # string, we have a winner
+ fi
+ done < $file # We're reading from the $file
+done