Skip to content

Commit a718a37

Browse files
committed
added linereader, reads a text file and prints a random non-repeating line from it until it's out of lines
1 parent a3b14ee commit a718a37

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

scripts/linereader

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
function die() { echo -e "$@" 1>&2 ; exit 1; }
4+
5+
if [ "x$1" == "x" ]
6+
then
7+
die "Need a filename for input."
8+
fi
9+
10+
MAGICFILE=$1
11+
MAGICBASE=`basename $MAGICFILE`
12+
INDEXFILE="/var/tmp/.lrscramble.$MAGICBASE"
13+
14+
if ! [ -e $MAGICFILE ]
15+
then
16+
die "No such file."
17+
fi
18+
19+
20+
LENTH=`wc -l $MAGICFILE | cut -d\ -f1`
21+
22+
function reshuffle() {
23+
for I in $(seq 1 $LENTH)
24+
do
25+
linez=( "${linez[@]}" $I )
26+
done
27+
linez=($(shuf -e "${linez[@]}"))
28+
printf "%s\n" "${linez[@]}" > $INDEXFILE
29+
}
30+
31+
[ -e $INDEXFILE ] || reshuffle
32+
NUMLINES=`wc -l $INDEXFILE | cut -d\ -f1`
33+
NUMCHARS=`wc -c $INDEXFILE | cut -d\ -f1`
34+
if [[ $NUMLINES -lt 1 ]] || [[ $NUMCHARS -lt 2 ]]
35+
then
36+
echo "Last invocation. Starting over."
37+
reshuffle
38+
fi
39+
40+
function nextnumber() {
41+
readarray -t scrambles < $INDEXFILE
42+
printf "%s\n" "${scrambles[0]}"
43+
scrambles=("${scrambles[@]:1}")
44+
printf "%s\n" "${scrambles[@]}" > $INDEXFILE
45+
}
46+
47+
function pline() {
48+
head -$1 $MAGICFILE | tail -1
49+
}
50+
51+
pline `nextnumber`

0 commit comments

Comments
 (0)