.!.
In my last post I was using if and else to look for each tag, and act on it. Tonight I’m going to convert that ugly mess to a case statement. It’s easier to read, and doesn’t have the hackish feel of if then, else.
I also learned a new trick that will help in parsing tags if the author mixes case. We’ll add a ‘shopt -s nocaseglob’ to the top of the shell script. This causes wildcard and regexp matches to be case insensitive. It does not, however, change the behaviour of commands like sed and grep. I’ve already seen a couple feeds there the tags were all upper case, and was worried about having to write some ugly regexp to match the tags.
#!/bin/bash
shopt -s nocaseglob
Read the rest of this entry
Share and Enjoy
.!.
The internets are slow tonight, and I’m tired, so I’ll just leave you with something quick. Tommorrow I’ll pick back up on the podcast script.
Let’s look at this short script:
#!/bin/bash
LIST=""
ls | while read FILE ; do
LIST="${FILE} ${LIST}"
done
echo $LIST
You may be suprised to find that LIST is empty after all that looping. This problem always mystified me until I learned about subshells. The pipe creates another shell, a subshell, where the loop executes. When the pipe ends, so does the subshell and all of it’s variables.
Fortunately there is a simple way around this. We need to execute the loop in the current shell, and redirect the command back into the loop from a subshell.
#!/bin/bash
LIST=""
while read FILE ; do
LIST="${FILE} ${LIST}"
done < <(ls)
echo $LIST
<(ls) creates an un-named pipe from the subshell into this shell. We can now redirect that pipe into the loop with another <.
If that got too confusing, try reading the Process Substitution chapter in the Advanced Bash-Scripting Guide.
Share and Enjoy
I’m working on a script randomly grab mp3s from a playlist and fill a CD with shuffled songs.
I’ll be using bash’s built-in random number generator which I believe is available in all versions.
Here’s the shuffle function for those interested:
# Usage: shuffle <filename>
function shuffle () {
cat "${@}"|grep -v "^#" | \
while read mp3 ; do
echo "$RANDOM $mp3"
done | sort | cut -d" " -f2-
}
Requirements will be mpg321 and cdrecord.
Share and Enjoy
Final installment of the podcast bash script for a while. I will eventually add the ability to auto-sync with my iPod, but I’m still undecided at which command line iPod tools I want to use. Currently gtkpod is doing a great job of keeping my iPod fed, and it’s simple to load it up, add my podcast folder and sync. gtkpod weeds out the dupes and automatically transfers just what’s needed.
I’m just going to describe the tagmp3 function, much like I did for the updated getmp3 function last time. Read the script to see how I hook the tagmp3 function into getmp3. It could all us a bit more error handling, but it works well enough now to be useful.
I am using id3v2 from here. It is easy to compile and only has one dependency, id3lib. I’m using the id3lib and id3lib-devel RPMs for FC3 from FreshRPMs.
Read the rest of this entry
Share and Enjoy
So far so good. The next exercise is to actually make this script useful by fetching the MP3s and arranging them into easy to understand directories.
The first order of business is to determine the name for the folder. I chose to use the channel name, and if that fails, the title of this episode. The later isn’t a very good choice as we could easily end up with a directory for each item in the feed.
Read the rest of this entry
Share and Enjoy
Last night we looked at parsing urls out of the feeds. Tonight I’m going look at parsing a little more information. Specifically the channel title, item title, item enclosure, and item pubDate.
Read the rest of this entry
Share and Enjoy