Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-04-2006, 03:39 AM   #1
LQ Newbie
Registered: Nov 2004
Location: USA
Distribution: Slackware Linux 9.1
Posts: 8

Rep: Reputation: 0
Help on iPod Shuffle Autofill BASH Script

Hi everyone:

I guess I only have some experience with BASH scripting, but so far, this has been my first major dive into it.

Anyways, here's a script that I'm working on. Using GNUpod-tools, it's basically a way to randomly fill an iPod Shuffle with songs:


# iPod Shuffle Autofill Script
# By Don Geronimo <dgeronimo [at] gmail [dot] com>
# This script removes all the songs contained on an
# iPod Shuffle, randomly picks a song from a list of
# songs, converts the song to an iPod-recognizable
# format (if necessary), and puts the song on the iPod
# Shuffle. It then randomly picks another song until
# it cannot add anymore songs (not enough disk space
# or all songs have been added [no duplicates])
# To make the file, use a command like this:
# $ find /directory/of/files -name *.ogg > autofill.m3u
# Ensure that in the database file, each newline has
# a path (absolute or relative) to a song; no blank lines
# or comment lines please
# Required Dependencies
#   * GNUpod (version which supports iPod Shuffle)

# Set the random seed, so it picks different
# songs everytime
SEED=`date +%N | sed -e 's/000$//' -e 's/^0//'`

# Define the iPod Shuffle mount point

# Define the list of all desired autofill songs

# Figure out number of songs on database
echo -ne "Number of songs to work with: "
echo -ne "$HOWMANYSONGS\n\n"

# Remove all the songs from the iPod Shuffle
echo -n "Removing all songs from iPod Shuffle ... "
gnupod_search -m $IPOD_DIR --delete $2>/dev/null
echo -ne "done!\n\n"

# Keep converting/adding songs until iPod Shuffle
# cannot hold any more songs.

while [ $CONTINUE -ne 0 ]

  # Randomly pick a song from the m3u playlist
  echo -ne "Loading song:\n"
  NUMBER=`expr $NUMBER + 1`
  RANDOMFILE=`tail +$NUMBER $DATABASE | head -n 1`
  echo -ne "$RANDOMFILE \n"

  # Add the file to the iPod Shuffle. Use --decode
  # to convert if necessary. Currently, this doesn't
  # work. I just have it set to ogg because all of my
  # music files are ogg vorbis
  if [ $EXTENSION = "mp3" -o $EXTENSION = "m4a" -o $EXTENSION = "m4b" ]
    gnupod_addsong -m $IPOD_DIR $RANDOMFILE $2>/dev/null
    gnupod_addsong -m $IPOD_DIR $RANDOMFILE --decode=mp3 $2>/dev/null

  # FIXME!: NOT WORKING; Read exit code. If not zero,
  # stop the loop.
  GNUPOD_EXIT=`echo $?`
  if [ $GNUPOD_EXIT -ne 0 ]


# Parse the GNUtunesDB file to the iPod
mktunes -m $IPOD_DIR $2>/dev/null
So far, it works to an extent. If any of you have time, could you offer some advice with what I can do to fix/improve this script? My two biggest problems with this right now is:
  • I wanted to use exit codes to determine if songs could no longer be added to the iPod Shuffle due to low disk space or all songs were already loaded. Unfortunately, that doesn't work once I actually tried the script on my iPod Shuffle. Initially, I was planning to use df to find the remaining space on the iPod Shuffle and probably ls to determine the file size of the music file and compare those, but I wasn't quite sure how to actually do that and there was no way for me to actually look at the file size because GNUpod converts the file and immediately tries to transfer it into the iPod Shuffle.
  • I need to also find a way for the script to detect the extensions of the music files. I'm at a loss with this, but I suppose it is not that important. Right now, I could just change the EXTENSION variable to determine if the conditional will work, but all of my music files are ogg vorbis so the variable doesn't really need to be changed.

If anyone could offer any help or suggestions, it would be greatly appreciated!
Old 01-04-2006, 07:19 AM   #2
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
1. Are you sure that gnupod is returning the status properly? Have you tried firing the command on the $ prompt & try echo $? Also, you can simply do this:

instead of this:

GNUPOD_EXIT=`echo $?`
2. If you need to get the extension from a filename (say of type a_b.mp3), you can try the following (there are various of ways of doing this):

Btw, what is the objective of $2>/dev/null?

gnupod_addsong -m $IPOD_DIR $RANDOMFILE $2>/dev/null
Old 01-04-2006, 01:26 PM   #3
LQ Newbie
Registered: Nov 2004
Location: USA
Distribution: Slackware Linux 9.1
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks much dustu76. The two suggestions are helpful for this script and maybe future scripts.

Are you sure that gnupod is returning the status properly?
Well, in my own stupidity from last night (that being really tired and its already 300 CST), I just assumed that if it didn't transfer the file, it would give an exit status of anything but 0; apparently, it still gives 0 even if it didn't transfer the file. So, I'll have to find some way to transfer the files now and stopping without relying on GNUpod's exit status.

Edit: Well, for the script, I am writing a way to see if there is enough space on the iPod Shuffle to add a song each time it tries to add a song. It's a function and it looks like this so far:

# F:Check if iPod Shuffle has enough memory for the file
function memorycheck {
  IPODMEMORY=`df -k | grep "$IPOD_DIR"`
  FILESIZE=`ls -kl $FILE`
This doesn't work - yet. While `df -k | grep "$IPOD_DIR"` and `ls -kl $FILE` will have the space available and the file size, respectively, it has other information that the script doesn't need:

df: /dev/hda4 2092376 56 2092320 1% /media/share
ls: rw-r--r-- don don 3287 2005-12-14 01:01 documents/music/only.ogg

In the df command, I only need the 4th column. The ls command I only need the 4th column as well. Can anyone suggest a way to get only those columns so the script will work?

Btw, what is the objective of $2>/dev/null?
I've seen this used in various scripts that I've seen before. It essentially takes the screen output (is that the term?) of a program and sends it to /dev/null; it pretty much silences the program a bit.

Last edited by dgeronimo; 01-04-2006 at 03:52 PM.
Old 01-04-2006, 06:23 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,569

Rep: Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427
throws away the stderr output, $2 would mean 2nd cmd line var


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
help mounting ipod shuffle carloscuderia Linux - Newbie 13 06-08-2007 08:44 AM
Ipod Shuffle help ziggis-soft General 8 12-29-2005 11:47 PM
ipod shuffle speel *BSD 2 06-10-2005 07:09 PM
ipod shuffle, again. JoeUser11 Linux - Newbie 8 05-17-2005 08:58 PM
ipod/ipod shuffle woes (lack of permissions to edit files) Lechium Linux - Software 1 04-13-2005 04:14 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:15 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration