LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 01-04-2006, 02:39 AM   #1
dgeronimo
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:

Code:
#!/bin/bash

# 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//'`
RANDOM=$SEED

# Define the iPod Shuffle mount point
IPOD_DIR=$1

# Define the list of all desired autofill songs
DATABASE=$2

# Figure out number of songs on database
echo -ne "Number of songs to work with: "
HOWMANYSONGS=`cat $DATABASE | wc -l`
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.

CONTINUE=1
while [ $CONTINUE -ne 0 ]
do

  # Randomly pick a song from the m3u playlist
  echo -ne "Loading song:\n"
  NUMBER=$RANDOM
  let "NUMBER %= $HOWMANYSONGS"
  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
  EXTENSION="ogg"
  if [ $EXTENSION = "mp3" -o $EXTENSION = "m4a" -o $EXTENSION = "m4b" ]
  then
    gnupod_addsong -m $IPOD_DIR $RANDOMFILE $2>/dev/null
  else
    gnupod_addsong -m $IPOD_DIR $RANDOMFILE --decode=mp3 $2>/dev/null
  fi

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

done

# 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, 06:19 AM   #2
dustu76
Member
 
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:

Code:
GNUPOD_EXIT=$?
instead of this:

Code:
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):

Code:
EXTENSION=${RANDOMFILE##*.}
Btw, what is the objective of $2>/dev/null?

Quote:
gnupod_addsong -m $IPOD_DIR $RANDOMFILE $2>/dev/null
HTH.
 
Old 01-04-2006, 12:26 PM   #3
dgeronimo
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.

Quote:
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:

Code:
# F:Check if iPod Shuffle has enough memory for the file
function memorycheck {
  IPODMEMORY=`df -k | grep "$IPOD_DIR"`
  FILESIZE=`ls -kl $FILE`
  if [ $IPODMEMORY -gt $FILESIZE ]
  then
    MEMORY_OK=1
  else
    MEMORY_OK=0
  fi
}
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?

Quote:
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 02:52 PM.
 
Old 01-04-2006, 05:23 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,228

Rep: Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023
Actually,
2>/dev/null
throws away the stderr output, $2 would mean 2nd cmd line var
 
  


Reply


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 07:44 AM
Ipod Shuffle help ziggis-soft General 8 12-29-2005 10:47 PM
ipod shuffle speel *BSD 2 06-10-2005 06:09 PM
ipod shuffle, again. JoeUser11 Linux - Newbie 8 05-17-2005 07:58 PM
ipod/ipod shuffle woes (lack of permissions to edit files) Lechium Linux - Software 1 04-13-2005 03:14 PM


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

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