The objectives of this lab experiment are to make you :
bash scripting PowerShell scriptingNote 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.
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:
id3tool fnm.mp3 | grep Album: | sed 's/^Album:[ \t]*//;s/ *$//'albumNm=$(id3tool fnm.mp3 | grep Album: | sed 's/^Album:[ \t]*//;s/ *$//')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:
id3tool fnm.mp3 | findstr -s Album: | % { $_ -replace '.*:[ \t]*' , '' }
$albumNm=id3tool fnm.mp3 | findstr -s Album: | %{$_ -replace '.*: *\t*',''}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
id3tool program binary is
already setup and is on your PATH.createTestMP3Files() is given
above. Enhance the procedure so that the MP3 files also have the
track number tag. sortMP3Files() {
createTestMP3Files TestMP3s
ls -l TestMP3s
mvMP3FilesToTheirAlbums TestMP3s $PWD/ResultMP3s
ls -lR TestMP3s ResultMP3s
}
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.
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
startOver() {
rm -fr TestMP3s ResultMP3s
}
sortMP3Files.sh . Source this file, and invoke the
procedures appropriately. Test thoroughly.In Windows
sortMP3Files.ps1 equivalent to the
above in the PowerShell syntax. Use standard commands that come with
Windows. Do not use Cygwin or other ports of Unix toolset. You must make use of procedures, perhaps one for each of the
tasks above. Feel free to use a different way of solving it in PS compared
to what you did in Linux. The content and structure of the .ps1 file
need not mirror that of the .sh file.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.
/home/ceg233/ceg23300/bin/id3tool.
A Windows exe of id3tool is also located at:
/home/ceg233/ceg23300/bin/id3tool.exe Recommended Tool.| . |
| Copyright © 2010 Prabhaker Mateti | last edited: October 30, 2010 |