aboutsummaryrefslogtreecommitdiff
path: root/challenge-098/abigail/bash
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-04 21:43:35 +0100
committerAbigail <abigail@abigail.be>2021-02-04 21:43:35 +0100
commit539905267d623f18b736a6d375360cff83ce45e9 (patch)
treec31f1924512ac2de6f6fbc2e97c638ffcfa5e7fb /challenge-098/abigail/bash
parent2026690ecabb7764509b7845898cb4ba2298eb99 (diff)
downloadperlweeklychallenge-club-539905267d623f18b736a6d375360cff83ce45e9.tar.gz
perlweeklychallenge-club-539905267d623f18b736a6d375360cff83ce45e9.tar.bz2
perlweeklychallenge-club-539905267d623f18b736a6d375360cff83ce45e9.zip
Bash solution for week 98, part 1
Diffstat (limited to 'challenge-098/abigail/bash')
-rw-r--r--challenge-098/abigail/bash/ch-1.sh54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-098/abigail/bash/ch-1.sh b/challenge-098/abigail/bash/ch-1.sh
new file mode 100644
index 0000000000..ebd4353079
--- /dev/null
+++ b/challenge-098/abigail/bash/ch-1.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-1.sh < input-file
+#
+# Each line of input consists of a filename and an amount
+# of characters to read.
+#
+
+#
+# Create associative arrays
+#
+declare -A content
+declare -A read
+
+function readN {
+ #
+ # Read arguments
+ #
+ filename=$1
+ amount=$2
+
+ #
+ # First time? Then read in file.
+ #
+ if [ "${read[$filename]}" != "1" ]
+ then read[$filename]=1
+ content[$filename]=$(<$filename)
+ fi
+
+ #
+ # Leading $amount characters
+ #
+ r=${content[$filename]:0:$amount}
+
+ #
+ # Remove $amount characters
+ #
+ content[$filename]=${content[$filename]:$amount}
+
+ echo $r
+}
+
+
+#
+# Read input, call readN
+#
+while read filename amount
+do readN $filename $amount
+done