This is just the stub of a script that will be used as part of my PCRadio OSS project. The purpose of the script is to query the user about how and where music is to be imported from and written to. At this point it only reads input and writes those preferences to file. Additionally, if the preferences file exists, it will read that in and provide those preferences as the default values the next time the script is called.

Comments welcome on how I can make it prettier/more efficient/etc. I'll update it as I get time to work on it.

Cheers!
Brion

Code:

#!/bin/bash

################ Name: importMusic.sh# Author: Brion Swanson# Desc: This script questions the user about the location of the music# from which it will be imported and to where it will be stored.## This script stores the answers of the response in a file called# .importPrefs in the user's home directory and will pre-populate# the responses to the questions if it exists.## Files will be moved from the source to the destination and renamed# according to the MP3/Ogg tagging if available. The format of the# file name can be configured by the user.###############

### writePrefs# Writes the user's preferences out to the ~/.importPrefs file.# These values will be read in as the default choices next time# the script is run.#####writePrefs() { # create an array to hold all the data to be written out # for ease and speed of writing declare -a state;

# add each piece of state data to the array in order state[0]="sourceDir=$SRCDIR"; state[1]="destDir=$DESTDIR"; state[2]="renameFiles=$RENAMEFILES"; state[3]="moveFiles=$MOVEFILES"; state[4]="filenameFormat=$FNAMEFORMAT"; state[5]="moveCmd=$MOVECMD";

# clear out the previous preferences file # and write a header to warn people not to modify by hand echo "## This is a generated file - changes made by hand will be lost ##" > ~/.importPrefs;

Update: I've added a command-line argument (-c --configure) to explicitly invoke the configuration questionaire. If the .importPrefs file already exists, then the preferences are pulled from it and the process runs automatically without any interaction from the user. If the the .importPrefs file does not exist or -c (--configure) is specified on the command-line then the preferences are read from the user and create/overwrite the existing .importPrefs file.

Problem: if I create a function to handle command-line argument parsing then I lose the command-line arguments $1 and so-forth. In the current code where command-line parsing occurs first it correctly determines the value of $DOCONFIG but for some reason the check for .importPrefs fails and sets the value of DOCONFIG to 'yes' when it should remain 'no' given the scenario: .importPrefs exists and -c was not passed on the command line. It should just produce "Done!' without doing anything.

Any suggestions are welcome.

Code:

#!/bin/bash

################ Name: importMusic.sh# Author: Brion Swanson# Desc: This script questions the user about the location of the music# from which it will be imported and to where it will be stored.## This script stores the answers of the response in a file called# .importPrefs in the user's home directory and will pre-populate# the responses to the questions if it exists.## Files will be moved from the source to the destination and renamed# according to the MP3/Ogg tagging if available. The format of the# file name can be configured by the user.###############

### writePrefs# Writes the user's preferences out to the ~/.importPrefs file.# These values will be read in as the default choices next time# the script is run.#####writePrefs() { # create an array to hold all the data to be written out # for ease and speed of writing declare -a state;

# add each piece of state data to the array in order state[0]="sourceDir=$SRCDIR"; state[1]="destDir=$DESTDIR"; state[2]="renameFiles=$RENAMEFILES"; state[3]="moveFiles=$MOVEFILES"; state[4]="filenameFormat=$FNAMEFORMAT"; state[5]="moveCmd=$MOVECMD";

# clear out the previous preferences file # and write a header to warn people not to modify by hand echo "## This is a generated file - changes made by hand will be lost ##" > ~/.importPrefs;

Another update. Fixed the problem with the previous test (forgot quotes around the variable), now I'm trying to get a case statement to fall through. This version has the case targets duplicated so it works, but I'd prefer -c fall through to --configure and just put the code in there once.

I promise this will do something useful soon.

Code:

#!/bin/bash

################ Name: importMusic.sh# Author: Brion Swanson# Desc: This script questions the user about the location of the music# from which it will be imported and to where it will be stored.## This script stores the answers of the response in a file called# .importPrefs in the user's home directory and will pre-populate# the responses to the questions if it exists.## Files will be moved from the source to the destination and renamed# according to the MP3/Ogg tagging if available. The format of the# file name can be configured by the user.###############

### writePrefs# Writes the user's preferences out to the ~/.importPrefs file.# These values will be read in as the default choices next time# the script is run.#####

writePrefs() { # create an array to hold all the data to be written out # for ease and speed of writing declare -a state;

# add each piece of state data to the array in order state[0]="sourceDir=$SRCDIR"; state[1]="destDir=$DESTDIR"; state[2]="renameFiles=$RENAMEFILES"; state[3]="moveFiles=$MOVEFILES"; state[4]="filenameFormat=$FNAMEFORMAT"; state[5]="moveCmd=$MOVECMD";

# clear out the previous preferences file # and write a header to warn people not to modify by hand echo "## This is a generated file - changes made by hand will be lost ##" > ~/.importPrefs;

dumpPrefs() { # create an array to hold all the data to be written out # for ease and speed of writing declare -a state;

# add each piece of state data to the array in order state[0]="sourceDir=$SRCDIR"; state[1]="destDir=$DESTDIR"; state[2]="renameFiles=$RENAMEFILES"; state[3]="moveFiles=$MOVEFILES"; state[4]="filenameFormat=$FNAMEFORMAT"; state[5]="moveCmd=$MOVECMD";

# now loop through the array writing out the data to the screen stateCount=${#state[*]} for (( i=0; $i != $stateCount; i++ )) do echo "${state[$i]}" done}

### rebuildFilename# Analyzes the MP3 or Ogg file's metadata and attempts to generate# an approprite file name based on the user's preferred filename# format (default: aRtist-Album-Track). Returns the generated name.#####

buildFilename() { echo "";}

### normalizeFilename# Normalizes filenames by removing apostrophes and quotes and converting# parentheses and spaces to underscores.#####

This update includes code to finally start renaming and tranferring files from $SRCDIR to $DESTDIR. It calls generateName.pl but I haven't written that yet so if you tell it to rename files it will fail. Still have a problem with the second use of 'define' but I'm working on that.

Code:

#!/bin/bash

################ Name: importMusic.sh# Author: Brion Swanson# Desc: This script questions the user about the location of the music# from which it will be imported and to where it will be stored.## This script stores the answers of the response in a file called# .importPrefs in the user's home directory and will pre-populate# the responses to the questions if it exists.## Files will be moved from the source to the destination and renamed# according to the MP3/Ogg tagging if available. The format of the# file name can be configured by the user.###############

### writePrefs# Writes the user's preferences out to the ~/.importPrefs file.# These values will be read in as the default choices next time# the script is run.#####

writePrefs() { # create an array to hold all the data to be written out # for ease and speed of writing declare -a state;

# add each piece of state data to the array in order state[0]="sourceDir=$SRCDIR"; state[1]="destDir=$DESTDIR"; state[2]="renameFiles=$RENAMEFILES"; state[3]="moveFiles=$MOVEFILES"; state[4]="filenameFormat=$FNAMEFORMAT"; state[5]="moveCmd=$MOVECMD";

# clear out the previous preferences file # and write a header to warn people not to modify by hand echo "## This is a generated file - changes made by hand will be lost ##" > ~/.importPrefs;

dumpPrefs() { # create an array to hold all the data to be written out # for ease and speed of writing declare -a state;

# add each piece of state data to the array in order state[0]="sourceDir=$SRCDIR"; state[1]="destDir=$DESTDIR"; state[2]="renameFiles=$RENAMEFILES"; state[3]="moveFiles=$MOVEFILES"; state[4]="filenameFormat=$FNAMEFORMAT"; state[5]="moveCmd=$MOVECMD";

# now loop through the array writing out the data to the screen stateCount=${#state[*]} for (( i=0; $i != $stateCount; i++ )) do echo "${state[$i]}" done}

### rebuildFilename# Analyzes the MP3 or Ogg file's metadata and attempts to generate# an approprite file name based on the user's preferred filename# format (default: aRtist-Album-Track). Returns the generated name.#####

buildFilename() { echo "";}

### normalizeFilename# Normalizes filenames by removing apostrophes and quotes and converting# parentheses and spaces to underscores.#####

Who is online

Users browsing this forum: No registered users and 4 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum