aboutsummaryrefslogtreecommitdiff
path: root/challenge-098/abigail/bash/ch-1.sh
blob: ebd4353079e3134491c952b7eab6242609c298a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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