playlists in linux shell script

With help from various scripts (that didn’t work) from this community, I’ve made a linux shell script to automatically create playlists for a Sansa Clip (V02.01.32F). Its functioning is explained in the code. It runs directly in the clip, and makes a playlist for every subdirectory of MUSIC.

I hope it’s any use to someone out there; this forum has helped me much and this is my way of saying “thanks”. 

Download http://www.nibla.nl/tmp/playlist.sh or

copy-and-paste:

#!/bin/bash
######################################################################
# Create sansa-playlists in linux-bash shell script
#
# Arguments: None.
#
# Author: Niels Blaauw
# Copyright: On a 20-line script!? You must be joking.
# Warning: Use at your own risk.
#
# Usage:
#   Plugin the sansa. Wait until it’s mounted.
#   Run the script from the command line, without arguments.
#
#   The script will try to change its path to the sansa-mountpoint.
#   It will ask whether it’s found the right path; you can cancel at
#   that point.
#
#  Description:
#
#     The script needs a directory structure like this:
#       /media/[disk]/MUSIC
#               /artist1
#                   /song1.mp3
#                   /song2.mp3
#                   /album1
#                       /song3.mp3
#                       /song4.mp3
#                       /song5.mp3
#                   /album2
#                       /song6.mp3
#                       /song7.mp3
#               /artist2
#                  /…
#
#     In that example, the script would have created playlists for
#     “artist1” and “artist2”. The first would be named:
#          …/MUSIC/artist1/artist1.m3u
#
#     and would contain:
#        #EXTM3U
#
#        song1.mp3
#
#        song2.mp3
#
#        album1\song3.mp3
#
#        album1\song4.mp3
#
#        album1\song5.mp3
#
#        album2\song6.mp3
#
#        album2\song7.mp3
#
#     ( bleep those double spaces!)
#     Note the backslashes: Sansa does NOT accept unix-slashes between
#     folders.
#     It DOES accept linux-returns though; don’t worry about LF/CR.
######################################################################
#set -x


#— Subroutine: List songs in folder and subfolders ----------
# Arguments:
#    $1: path to be prepended to songs

list_songs()
{
   ls | while read song_or_album
   do
      if ["echo $song_or_album | egrep '\.mp3$|\.ogg$|\.wav$'" != “”]
      then
         #— list song in m3u file -------------------------
         echo $1$song_or_album >> $targetfile
         echo “”               >> $targetfile
         echo “Added to $artist: " $1$song_or_album
      elif [-d $song_or_album]
      then
         #— go to deeper directory; call self -------------
         next_prefix=$1$song_or_album”\"
         cd $song_or_album
         list_songs $next_prefix
         cd …
      fi
   done
}

# MAIN***************************************************

#— Dirty trick: Change directory to within the sansa ---------
cd /media/*/MUSIC
workdir=pwd

#— Ask for user confirmation that directory is right ---------
echo "Going to work on $workdir. Press enter to continue, Ctrl-C to abort"
read answer

#— Create list ----------------------------------
ls | while read artist
do
   if [-d $artist]
   then
      cd $artist
      targetfile=$workdir/$artist/$artist.m3u
      echo “#EXTM3U”   > $targetfile
      echo “”         >> $targetfile
      list_songs “”
      cd …
   fi
done

Thanks for posting this–that’s very nice of you.   :slight_smile: