Note that there are some explanatory texts on larger screens.

plurals
  1. POBash: only last iteration of loop index not being stored as new element of array
    primarykey
    data
    text
    <p>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".</p> <pre><code>(more output above ...) --&gt;./Hip-Hop/OutKast/Stankonia/Toilet Tisha.aif&lt;-- GENRE: Hip-Hop ARTIST: OutKast ALBUM: Stankonia TITLE: Toilet Tisha --&gt;./Electronic/Squarepusher/Hard Normal Daddy/Cooper's World.aif&lt;-- GENRE: Electronic ARTIST: Squarepusher ALBUM: Hard Normal Daddy TITLE: Cooper's World --&gt;./Electronic/Squarepusher/Hard Normal Daddy/Papalon.aif&lt;-- GENRE: Electronic ARTIST: Squarepusher ALBUM: Hard Normal Daddy TITLE: Papalon --&gt;./Electronic/Squarepusher/Hard Normal Daddy/Vic Acid.aif&lt;-- GENRE: Electronic ARTIST: Squarepusher ALBUM: Hard Normal Daddy TITLE: Vic Acid --&gt;./Electronic/Squarepusher/Go Plastic/Go! Spastic.aif&lt;-- GENRE: Electronic ARTIST: Squarepusher ALBUM: Go Plastic TITLE: Go! Spastic --&gt;./Electronic/Squarepusher/Go Plastic/Greenways Trajectory.aif&lt;-- GENRE: Electronic ARTIST: Squarepusher ALBUM: Go Plastic TITLE: Greenways Trajectory --&gt;./Electronic/Squarepusher/Go Plastic/My Red Hot Car.aif&lt;-- GENRE: Electronic ARTIST: Squarepusher ALBUM: Go Plastic TITLE: My Red Hot Car --&gt;./Electronic/Squarepusher/Feed Me Weird Things/Kodack.aif&lt;-- GENRE: Electronic ARTIST: Squarepusher ALBUM: Feed Me Weird Things TITLE: Kodack --&gt;./Electronic/Squarepusher/Feed Me Weird Things/North Circular.aif&lt;-- GENRE: Electronic ARTIST: Squarepusher ALBUM: Feed Me Weird Things TITLE: North Circular --&gt;./Electronic/Squarepusher/Feed Me Weird Things/Tundra.aif&lt;-- GENRE: Electronic ARTIST: Squarepusher ALBUM: Feed Me Weird Things TITLE: Tundra </code></pre> <p>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:<img src="https://i.stack.imgur.com/v4g2n.png" alt="enter image description here"></p> <pre><code>#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 "--&gt;$music_file&lt;--"; 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 "" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload