From 6d0ccb2c3e17a270f882d04098b3495d903a4b9d Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 12 Feb 2021 17:24:05 +0100 Subject: Bash solution for week 4, part 2 --- challenge-004/abigail/README.md | 1 + challenge-004/abigail/bash/ch-2.sh | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 challenge-004/abigail/bash/ch-2.sh 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 -- cgit