180 likes | 304 Views
Lecture 9 – Scripting II. Testing scripts MP3 files id3tool. Script Development. Plan your scripts Source script files Make “proper” functions “echo” your code <bold>Mystery Hint</bold>. 1. Plan Your Scripts. Hastily-written scripts suck to debug Finding code snippets can be hard
E N D
Lecture 9 – Scripting II • Testing scripts • MP3 files • id3tool
Script Development • Plan your scripts • Source script files • Make “proper” functions • “echo” your code • <bold>Mystery Hint</bold>
1. Plan Your Scripts • Hastily-written scripts suck to debug • Finding code snippets can be hard • “Analogous” code can be implemented differently (i.e., orders of params, etc)
2. Source Script Files • Place functions into script file • Instead of “main” at bottom, just source the script • Call functions by hand at terminal
3. Make “Proper” Functions • Collect all related code into a function • If function is too large, break into sub-functions • Subjective, but 10-15 lines • Even a single line of code is okay • Could be called again • Could be a very complex line
4. “echo” Your Code • Sometimes a line doesn’t seem to be working • Placing an ‘echo’ on line can help • You’ve been told to never debug like this…why?
4. “echo” Your Code • Sometimes a line doesn’t seem to be working • Placing an ‘echo’ on line can help • You’ve been told to never debug like this…why? • Alters pointer/stack setup • We’re not playing with pointers, so we’re (probably) good
5. Walk Away • Anime characters fight better when they’re mad • Scientists/Engineers don’t code better when mad • You’re not an anime character
MP3 Files • We don’t care about the internals • JPEG6.2 is complex enough • Audio is worse! • What do we care about?
MP3 Files • We don’t care about the internals • JPEG6.2 is complex enough • Audio is worse! • What do we care about? • ID3 tags!
ID3 Tags • They contain basic info: • Title • Artist • Album • Year • Track Number • etc
id3tool • Actual audio is complex • Let’s ignore it • ID3 tags are easy • Let’s use id3tool to view/alter it
id3tool • id3tool <filename.mp3> • Lists attributes of the MP3 file • Can set attributes • -a album • -r artist • -y year • File must already exist! • “touch” command
BASH Function #1 createOneDummyMP3() { touch $1 id3tool $1 -a $2 -r $3 -y $4 }
BASH Function #2 createTestMP3Files() { mkdir –p $1 pushd $1 for iin {0..9} do for j in {0..9} do createOneDummyMP3 song$i$j.mp3 Album$iArtist$j 200$i done done popd }
PS Function #1 function createOneMP3File( $filename, $album, $artist, $year) { echo ‘’ > $filename id3tool $filename -a $album -r $artist -y $year }
PS Function #2 function createTestMP3Files( $dirName) { mkdir –force $dirName pushd $dirName foreach ($i in 0..9) { foreach ($j in 0..9) { createOneMP3File song$i$j.mp3 Album$i Artist$i 200$i } } popd }