-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsong_search
executable file
·47 lines (39 loc) · 1.21 KB
/
song_search
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
#!/usr/bin/env bash
# Searches the contents of all of the files in my lyrics directory for the
# argument word or phrase and prints the name of any matches (Artist - Title)
# The -p(lay) argument runs the play_song script (which searches for an appropriate
# local file to play and plays it) and means that if multiple lyric matches are found
# you will be prompted to choose between them.
if [ "$1" = "-p" ] ; then
shift
play_song=true
else
play_song=false
fi
search_string="$@"
counter=0
readarray matches < <(grep -ilo "$search_string" ~/music/lyrics/*)
for match in "${matches[@]}"
do
echo -e "\n$counter)" $(basename "${match}" | sed 's/-/ - /')
(( counter=$counter + 1 ))
done
echo
if [ $counter = 0 ]; then
echo -e 'No matching lyrics found\n'
exit 0
fi
if [ $play_song = false ]; then
exit 0
fi
if [ $counter = 1 ]; then
selected_song=${matches[0]}
else
echo -e 'Select which song you want to play:'
read chosen_index
selected_song=${matches["$chosen_index"]}
fi
artist_title="$(basename "$selected_song")"
artist="$(echo "$artist_title" | awk -F- '{print $1}')"
title="$(echo "$artist_title" | awk -F- '{print $2}')"
play_song "$artist" "$title"