CEG 233: Linux and Windows 

Lab on Scripting with Bash and PowerShell

   

Table of Contents

  1. Educational Objectives
  2. Background
  3. Lab Experiment
  4. Acknowledgements
  5. References

Educational Objectives

The objectives of this lab experiment are to make you :

  1. Familiar with a few command line utilities
  2. Learn fundamentals of Linux bash scripting
  3. Learn fundamentals of Windows  PowerShell scripting

Background

Note the Required Readings in the References.

This Lab is about scripting.  In an earlier lab on scripting, you have already used both Bash and PowerShell interactively, and even wrote a few short procedures.  In this lab, the complexity of scripting is deeper.

This lab uses MP3 files as a domain where scripting can be usefully deployed.  It is not crucial that you understand the internal structure of MP3 files.  It is sufficient to know that the ID3 tags are embedded in the MP3 files.  For further details on MP3 files, read the Wiki article mentioned in the References.

You will be creating a bunch of MP3 files in a TESTMP3S directory.  These are "artificially" created, not playable "music" files.  The tool we use to manipulate, namely id3tool in Linux and also  in Windows, does not touch the musical content.  The tool manipulates only the ID3V1 tags.  Invoking id3tool with no arguments displays its usage. Invoking id3tool with the name of an MP3 file as its only argument displays its ID3V1 tags. See the References for download details of the tool.

For our experimental purpose, this TESTMP3S will contain about a 100 files.  These files do have at least Album, Artist, and Year tags.  They may or may not have a track number or title tags. Assume that none of our tags have white spaces. You will be creating subdirectories based on the Album tag, and moving into each album directory the files of that album.  You will then be renaming each file based on the track number, album, and artist as in nn-Album-Artist.mp3.


Script Development and Testing Hints


Bash Hints

Here is an example of how a dummy MP3 file (name = first argument) whose tags are Album (second argument), Artist (third argument), Year (fourth argument) can be created in the current working directory. 

createOneDummyMP3File()
{
  echo > $1
  id3tool $1 -a $2 -r $3 -y $4
}

Obviously, in creating 100 dummy files, we need to come up with different file names, album names, etc.  In the procedure below we just use names such as song47, ..., Album1, ..., Artist1, ....  Note that none of these have a track number. This procedure creates a subdirectory, whose name is given as the first argument, in the current working directory, and populates it with about a 100 dummy MP3 files as described in the Background.

Be more creative than this!  You may earn bonus points!

createTestMP3Files() {
    mkdir -p $1
    pushd $1
    for i in {0..9}
    do
	for j in {0..9}
	do
	    createOneDummyMP3File song$i$j.mp3 Album$i Artist$j 200$i
	done
    done
    popd
}

Exercise:  Discover what the following do:


PowerShell Hints

The following are PowerShell equivalents to the above in Bash.  Study the differences in syntax very carefully.

function createOneDummyMP3File($fileName, $album, $artist, $year)
{
  echo '' > $fileName
  id3tool $fileName -a $album -r $artist -y $year
}
PowerShell functions list their parameters with $names.
function createTestMP3Files($dirName) {
    mkdir -force $dirName
    pushd $dirName
    foreach ($i in 0..9) {
	foreach ($j in 0..9) {
	    createOneDummyMP3File song$i$j.mp3 Album$i Artist$j 200$i
	}
    }
    popd
}

Exercise:  Discover what the following do:


Lab Experiment

All work is expected, but not required, to be carried out in the Operating Systems and Internet Security (OSIS) Lab, 429 Russ. But, you are welcome to work wherever. Note that use of both Linux and Windows and other software, that may not always be installed in other facilities, may be needed.

This lab depends on the following topics covered in the following lectures: file name regular expressions, procedures in  Bash and  PowerShell,  Bash script file details, and  PowerShell script file details.   Before beginning to work on this lab, you must have read the notes on  Bash and  PowerShell given in the References.

In Linux

  1. The id3tool program binary is already setup and is on your PATH.
  2. A bash procedure called createTestMP3Files() is given above.  Enhance the procedure so that the MP3 files also have the  track number tag. 
  3. Study the bash procedure shown below.  
    sortMP3Files() {
        createTestMP3Files TestMP3s
        ls -l TestMP3s
        mvMP3FilesToTheirAlbums TestMP3s $PWD/ResultMP3s
        ls -lR TestMP3s ResultMP3s
    }
  4. You will be developing the mvMP3FilesToTheirAlbums procedure. It "sorts" and moves MP3 files located in the source directory given as the first argument into the destination directory given as the second argument.  Do not assume the source directory is exactly as created by  createTestMP3Files .  Note how we are making sure that the second argument is a full path name.  The mvMP3FilesToTheirAlbums should od the following.
    1. Create a subdirectory in the destination directory for each album name seen among the MP3 files of the source directory.  If such a directory already exists in the destination directory, do not complain.
    2. Move an MP3 file with an album tag of AlbumX of the source directory into AlbumX subdirectory of the destination directory.  If a file with that name already exists, we let it be overwritten.
    3. Rename the MP3 files of album directories so that they are now of the form nn-Album-Artist.mp3.  If an MP3 file has no track number tag, give it a default tag number, say 99.
      mv $mp3fnm $trackNum-$albumName-$artistName.mp3
  5. You will find the following procedure useful during testing.
    startOver() {
        rm -fr TestMP3s ResultMP3s
    }
  6. Include all the procedures in a script file named sortMP3Files.sh . Source this file, and invoke the procedures appropriately.  Test thoroughly.

In Windows

Turnin

Note the number <n> of this Lab from the course home page and use L<n> as the first argument to turnin.  Read the Grading Sheet to determine what files must be submitted.

For 20 max bonus points, show us the evidence that you used your script on your collection of 100+ real MP3 files.

Link to Grading Sheet


Acknowledgements


References

  1. Prabhaker Mateti, Notes on Scripting with BASH, scriptingWithBASH.html.  Required Reading.
  2. Prabhaker Mateti, Notes on Scripting with  PowerShell, scriptingWith PowerShell.html.   Required Reading.
  3. Sarah Gothard, bashVsPowerShellTable.html  Required Reading.
  4. ID3 Tags http://en.wikipedia.org/wiki/Id3  Recommended Reading.
  5. id3tool is an open source contribution of http://nekohako.xware.cx/id3tool/ A program binary for Ubuntu is located at:  /home/ceg233/ceg23300/bin/id3tool.  A Windows exe of id3tool is also located at: /home/ceg233/ceg23300/bin/id3tool.exe  Recommended Tool.
  6. sed for Windows is an open source contribution of http://gnuwin32.sourceforge.net/packages/sed.htm Recommended Tool.

.
Copyright © 2010 Prabhaker Mateti last edited: October 30, 2010