Question

Lab 1: Dissection The aims of this lab are to: 1. Gain fluency in command line operations, creating and editing files etc. 2. Look at a working shell script to

see how it achieves what it does. 3. Experiment with fragments at the command line and write ones own files to learn the "tricks of the trade" The Script The simple script we are going to look at is one that I wrote a while ago to help put a bunch of audio files from my audio server onto my phone so I can play them over Bluetooth while travelling. When I buy a recording either it comes on CD or I buy a download. Either way, the files end up as a boxed set on my media server. If there are more than one CDs in the box, they will each end up in their own directory like this: $ ls -l 'Oscar Peterson: Songbooks' total 8 drwxr-xr-x 2 nick nick 4096 May 7 2011 'Oscar Peterson-Songbooks (Count Basie, Warren & Youmans)' drwxr-xr-x 2 nick nick 4096 May 7 2011 'Oscar Peterson-Songbooks (Duke Ellington)' and each directory has a flac (Free Lossless Audio Codec) file for each track in the album, because I don't wan to put up with lossy audio when I'm listening at home. Problem: my phone's Music app won't play flac files. It will play ogg/vorbis files so quite often I want to transcode a lot of flac files to vorbis so I can upload them to my phone's SD card and carry them around with me. I want to do this quickly, using all the cores on the computer at the same time (and even cheap computers have at least 4, usually, these days). Problem characteristics: if this were done manually, we'd be traversing directory trees using cd, and issuing commands to transform flac to ogg in the background. You can download the project from github with the command git clone https://github.com/nickbailey/flactree If git isn't installed on your system, visit the link and download the files (code, click the file, raw, save as...). The Assignment Understand how the script works and write an explanation in essay form. You can use any resource to work out what commands do. There are some on the moodle and hints in this sheet, but searching on google, stack overflow etc etc is encouraged provided you can demonstrate you understand the result. Answer the questions below in your explanation and include example scripts of commands of your own devising to illustrate your explanation. Bonus credit: Implement some or all of the TODO points (this is advanced and you can get good marks without attempting it. Do it to is a necessary, but not sufficient, condition to obtain grades in the A1-A3 range). Specific Questions These are to guide you through your investigation. When you write up the report, try to make it a narrative. Don't feel you have to slavishly answer the questions 1, 2, 3, ... etc. 1. What is happening on lines 11 and 15? How to you use the value associated with TARGET and THREADS in a script (for example, can you print out the value of THREADS?) 2. In the shell, functions can be defined but the syntax is quite unusual here. Find an example of a function. 3. Instead of an argument list, one way of using shell functions is to use the special values $0, $1, ..., $9 which work like the first 10 elements of the python list argv we discussed in the lectures. That's what's used in this script. There are also special variables (not used in this script) called #, @ and *. Find out what these do and write a few example functions to show you understand them. 4. The body of the program consists of a pair of loops of the form: find startpoint qualification | while read d ; do loop body Look up how the find command works. What is the pipe and while read command doing? 5. Why is this method adopted? Hint: think about filenames with unfriendly characters in them. 6. Explain how a directory hierarchy is copied in lines 27 to 31. What to you end up with after these lines have run? 7. Let's think about the sanitise_path. What is the reason this is needed? Hint: what happens if you try to create a file called ;:; on your phone? 8. Read up on the sed command. sanitise_path uses it to to a search and replace on a string. Find out the syntax for a global search and replace sed command and tests it out on the terminal. 9. [] in sed regular expressions means a set of characters (so [AaEeliOoUu] matches one vowel, upper or lower case). Give some examples of what sanitise_path returns for different inputs. You can try it out on the terminal. 10. Try making a dummy directory hierarchy with some “audio tracks” and running the script up to line 32. See what happens. Is that what you expected? Hint: you can force early termination with the exit command. Hint 2: the "audio tracks" don't have to be audio tracks. Empty files with a .flac extension are fine. I expect you'll be using mkdir, cd and touch commands (among others). 11. Perform a similar investigation for the code in lines 35 to 51, thinking about what you expect before you actually run it. 12. What do lines 43 and 50 do? 13. Look up the test command. Notice that it can also be called as [ (yes, [ ). What does the first part of line 48 do? 14. Conditional expressions, just like C++, are only evaluated as far as they need to be. In many languages including C++ and shell, && is a boolean and, and || is a boolean or. So if we have conditional expressions C1, C2, C3, then C1 && C2 && C3 will only evaluate left to right until one of the expressions is false (because then it's known that the whole expression is false). So what is line 48 doing? 15. make is a command widely used compiling larger systems. We'll come back to make lots when we move on to C++ programming. It can run several commands in parallel on multicore machines to speed things up. It's used in line 57. You'll have to fix the command line to get it to work. Find out what the -f, -j and -C flags to make do. 16. Look at the makefile this script uses. The indented lines are shell commands, and the rest are dependency rules. You'll look at the latter in much more detail when we're building C++ programs. Lines 7 and 8 gives make the recipe for changing an ogg file into a flac file. The magic command is on line 8. The leading @ means don't print the command before running it; the make variables @ and ? are the file being created and the thing it's created from (so resp. the ogg and flac files). You won't have any ogg files (except empty ones), and probably won't have oggenc installed either, so change the rule accordingly. Maybe just copy the flac to the ogg file? They are empty anyway.