I have a script that is searching a directory tree for music files, extracting the relevant information from the full path name of music files according to specific attributes (Genre, Artist, Track, Album), storing the formatted substrings as elements in an array, then sorting the elements of the array based on the option which was passed on the command line (e.g. sort by album). Everything seems to be working nicely, except the resulting output of the last iteration of the for loop doesn't seem to be getting stored as a new element in the array. After looking up information on Bash arrays, I found that there is no limitation on the array size. So I am left scratching my head as to why every output of every other iteration up until the last is getting stored in the array. If you look at the output below, you can see that the last element should be the track "Tundra".
(more output above ...)
-->./Hip-Hop/OutKast/Stankonia/Toilet Tisha.aif<--
GENRE:
Hip-Hop
ARTIST:
OutKast
ALBUM:
Stankonia
TITLE:
Toilet Tisha
-->./Electronic/Squarepusher/Hard Normal Daddy/Cooper's World.aif<--
GENRE:
Electronic
ARTIST:
Squarepusher
ALBUM:
Hard Normal Daddy
TITLE:
Cooper's World
-->./Electronic/Squarepusher/Hard Normal Daddy/Papalon.aif<--
GENRE:
Electronic
ARTIST:
Squarepusher
ALBUM:
Hard Normal Daddy
TITLE:
Papalon
-->./Electronic/Squarepusher/Hard Normal Daddy/Vic Acid.aif<--
GENRE:
Electronic
ARTIST:
Squarepusher
ALBUM:
Hard Normal Daddy
TITLE:
Vic Acid
-->./Electronic/Squarepusher/Go Plastic/Go! Spastic.aif<--
GENRE:
Electronic
ARTIST:
Squarepusher
ALBUM:
Go Plastic
TITLE:
Go! Spastic
-->./Electronic/Squarepusher/Go Plastic/Greenways Trajectory.aif<--
GENRE:
Electronic
ARTIST:
Squarepusher
ALBUM:
Go Plastic
TITLE:
Greenways Trajectory
-->./Electronic/Squarepusher/Go Plastic/My Red Hot Car.aif<--
GENRE:
Electronic
ARTIST:
Squarepusher
ALBUM:
Go Plastic
TITLE:
My Red Hot Car
-->./Electronic/Squarepusher/Feed Me Weird Things/Kodack.aif<--
GENRE:
Electronic
ARTIST:
Squarepusher
ALBUM:
Feed Me Weird Things
TITLE:
Kodack
-->./Electronic/Squarepusher/Feed Me Weird Things/North Circular.aif<--
GENRE:
Electronic
ARTIST:
Squarepusher
ALBUM:
Feed Me Weird Things
TITLE:
North Circular
-->./Electronic/Squarepusher/Feed Me Weird Things/Tundra.aif<--
GENRE:
Electronic
ARTIST:
Squarepusher
ALBUM:
Feed Me Weird Things
TITLE:
Tundra
As you can see, the last iteration in the DEBUG section should be the title "Tundra". However, when I display the contents for array "track_list" every track is printed in the desired format except for "Tundra" (look at attached .png file). Does anyone have any idea as to why this might be? Here is a portion of my script:
#more code above ...
#create arrays
declare -a track_list
declare -a directory_contents
#populate directory with files
cd $directory
directory_contents=$(find . -mindepth 1 -type f)
cd ..
IFS=$'\n'
#store each file of directory in track_list
for music_file in ${directory_contents[*]}; do
if [ -n "$DEBUG" ] ; then echo "-->$music_file<--"; fi
this_genre=$(echo $music_file | awk -F"/" '{print $2}')
this_artist=$(echo $music_file | awk -F"/" '{print $3}')
this_album=$(echo $music_file | awk -F"/" '{print $4}')
this_title=$(echo $music_file | awk -F"/" '{print $5}' |\
awk -F".aif" '{print $1}' || awk -F".mp3" '{print $1}' ||\
awk -F".wav" '{print $1}' || awk -F".flac" '{print $1}' \
|| awk -F".m4a" '{print $1}')
function artist_list
{
track=$(printf "%-20s\t\t%-30s\t\t%-30s\t\t%-10s\n"\
"$this_artist" "$this_title" "$this_album" "$this_genre")
track_list=("${track_list[*]}" $track)
}
if [[ $genre = "true" ]] ; then
track=$(printf "%-10s\t\t%-20s\t\t%-30s\t\t%-30s\n"\
"$this_genre" "$this_artist" "$this_title" "$this_album")
track_list=("${track_list[*]}" $track)
elif [[ $artist = "true" ]] ; then
artist_list
elif [[ $album = "true" ]] ; then
track=$(printf "%-30s\t\t%-20s\t\t%-30s\t\t%-10s\n"\
"$this_album" "$this_artist" "$this_title" "$this_genre")
track_list=("${track_list[*]}" $track)
elif [[ $title = "true" ]] ; then
track=$(printf "%-30s\t\t%-20s\t\t%-30s\t\t%-10s\n"\
"$this_title" "$this_artist" "$this_album" "$this_genre")
track_list=("${track_list[*]}" $track)
else
artist_list
fi
if [ -n "$DEBUG" ]; then
echo "GENRE:"
echo $this_genre
echo "ARTIST:"
echo $this_artist
echo "ALBUM:"
echo $this_album
echo "TITLE:"
echo $this_title
echo ""
fi
done
unset IFS
if [[ $genre = "true" ]] ; then
./mulib g
elif [[ $artist = "true" ]] ; then
./mulib a
elif [[ $album = "true" ]] ; then
./mulib m
elif [[ $title = "true" ]] ; then
./mulib t
else
./mulib
fi
echo "$track_list" | sort
echo ""
set -xat the top of the script to produce a trace of every line as it's run, and trying to find where the trace output starts looking wrong.