-
Notifications
You must be signed in to change notification settings - Fork 9
/
randomblock.sh
executable file
Β·49 lines (43 loc) Β· 1.8 KB
/
randomblock.sh
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
#!/bin/bash
# π»πποΈ Keep or Sweep for notes!
# Show a random block of text from a notes random notes file
# Simply make executable and click (or run as bash randomblock.sh)
# http://keeporsweep.net
# Use the current folder for now
FOLDER=$(pwd)
# Get a random file, ignoring hidden files
# https://askubuntu.com/questions/266179/how-to-exclude-ignore-hidden-files-and-directories-in-a-wildcard-embedded-find/749708#749708
# "tr -d '\0'" gets rid of "command substitution: ignored null byte in input"
# https://stackoverflow.com/questions/46163678/get-rid-of-warning-command-substitution-ignored-null-byte-in-input/46163991#46163991
RANDOMFILE=$(find "$FOLDER" -not -path '*/\.*' -type f -print0 | shuf -zn1 | tr -d '\0')
# Separate note files to blocks, give out one random block
# Code from https://stackoverflow.com/a/18539730
i=1
s=1
declare -a arr
while read -r line
do
# If we find an empty line, then we increase the counter (i),
# set the flag (s) to one, and skip to the next line
[[ $line == "" ]] && ((i++)) && s=1 && continue
# If the flag (s) is zero, then we are not in a new line of the block
# so we set the value of the array to be the previous value concatenated
# with the current line
[[ $s == 0 ]] && arr[$i]="${arr[$i]}
$line" || {
# Otherwise we are in the first line of the block, so we set the value
# of the array to the current line, and then we reset the flag (s) to zero
arr[$i]="$line"
s=0;
}
done < "$RANDOMFILE"
# Choose index to be a random number from 0 to the length of the array
# Code from https://stackoverflow.com/a/2388555
# Modified to skip empty blocks
selectedblock=""
while [ "$selectedblock" == "" ]; do
selectedblock=${arr[$RANDOM % ${#arr[@]} ]}
done
echo "$RANDOMFILE"
echo ""
echo "$selectedblock"