Wednesday, September 15 Exercises using shell variables, conditionals and foreach loops # you could store all your information in files % echo 1 > n % cat n 1 % rm n % echo 2 > n % cat n 2 # in many cases however shell variables are a simpler solution % set str = one # use echo to look at the value of the variable % echo $str one % set n = 1 % echo $n 1 # shell variables can be assigned lists (or vectors or arrays) % set str = "one two three four" # looks sort of like a list to me % echo $str one two three four # even acts a little like a list % echo $str[1] one two three four # unfortunately not enough like % echo $str[2] <<<< str: Subscript out of range. # creating real lists requires some special syntax % set lst = ( one two three four ) # at first blush the variable behaves as above % echo $lst one two three four # but now we can pick out the individual elements % echo $lst[1] one % echo $lst[2] two % echo $lst[4] four # however we can't pick out elements that don't exist % echo $lst[5] lst: Subscript out of range. # we can also change the elements to have new values % set lst[4] = five % echo lst[4] five # and we can even shift the items in a list to the left % shift lst % echo $lst[1] two % in shifting left we eliminated one item in the list % echo $lst[4] lst: Subscript out of range. % and the 4th item in the list became the 3rd item % echo $lst[3] five # creating a list of files using the backtick operator (`) % ls *.mp3 bo_diddley.mp3 james_brown.mp3 % set files = ( `ls` ) % echo $files bo_diddley.mp3 james_brown.mp3 % echo $files[1] james_brown.mp3 % echo $files[2] james_brown.mp3 # doing simple math calculations in a shell script # setting variables and doing simple integer math % set x = 1 % echo $x 1 % @ x = $x + 1 echo $x 2 % @ x++ % echo $x 3 # dealing with real numbers (floats) requires other tools # the shell can't handle real numbers % @ x = 7.5 * 3.14 @: Badly formed number # using 'awk' to do math with real numbers % echo 7.5 | awk '{ print ( 3.14 * $1 ) }' 23.55 # using 'bc' an arbitrary precision calculator % set pi = 3.14 % set diameter = 7.5 % echo "$pi * $diameter" | bc 23.55 # conditional statements allow shell scripts to make choices % ls *.mp3 bo_diddley.mp3 james_brown.mp3 % if ( -e bo_diddley.mp3 ) echo "Hey Bo Diddley!" Hey Bo Diddley! # here's a qualified conditional statement: ! = negation % if ( ! -e wayne_newton.mp3 ) echo "Yo! No Wayne." "Yo! No Wayne." # a multiple-line conditional statement with an else clause % if ( ! -e james_brown.mp3 ) then if? echo "Get up off of that thing and dance." if? else echo "Papa's got a brand new bag!" Papa's got a brand new bag! # the antecedent can be any command that you like % if ( -e bo_short.wav ) esdplay bo_short.wav # a compound conditional statement: && = conjunction % if ( -e james_brown.mp3 && -e bo_diddley.mp3 ) then % echo "Dance! You'll feel better." Dance! You'll feel better. # 'foreach' statements are for looping over lists % cd bo_tracks % ls bo_backward.wav bo_forward.wav bo_slowly.wav % foreach file ( `ls` ) foreach? echo $file foreach? esdplay $file foreach? end bo_backward.wav bo_forward.wav bo_slowly.wav % In case you're interested, here's some info on working with sounds. The command 'mpg123' plays audio files in MPEG 1.0/2.0 format (layers 1, 2 and 3). MPEG files have to be decoded before they can be played and 'mpg123' enables you to convert an audio file to a format that can be handled by other command line tools. The -s options pipes the output to the standard output as raw (headerless) linear PCM audio data, 16 bit, stereo, host byte order. The -w option converts the file to WAV format (used by Microsoft) and sends it to the standard output (use - instead of a file name) or writes it to a specified file. This command also permits you to speed up the sound (-d n) by only playing every n'th sample or slow it down (-h n) by playing each sample n times. You can learn more about 'mpg123' at its web site (http://www.mpg123.de/). If you really want to manipulate sound files using fancy filters and synths, learn about 'sox' (for Sound Exchange) (http://sox.sourceforge.net/), a command line tool that provides lots of filters, special effects, mixers and synthesizers to play with. You can use 'sox' to reverse the samples in a sound file which is particularly useful in looking for satanic verses hidden in music files. Another useful sound library, called Elightened Sound Daemon (http://www.tux.org/~ricdude/overview.html) provides tools for playing sounds. All of these commands are available on the department Linux machines, but note that while these machines have soundcards (which are necessary for generating sound) they don't have speakers and so you'll have to bring headphones if you want to experiment with sound. Here are some short tips. Use 'info' and 'man' to learn more: mpg123 - MPEG decoder mpg123 -w file (--wav) # write file in wav format (- for standard output) mpg123 -s file (--stdout) # write file in headerless PCM, 16bits, host byte order mpg123 -d n (--doublespeed) # play every nth sample mpg123 -h n (--halfspeed) # play every sample n times mpg123 -h 4 bo_diddley.mp3 # here's how I made some of the tracks mpg123 --wav bo_slowly.wav --halfspeed 8 bo_diddley.mp3 SoX - sound exchange - universal sound sample translator sox in.wav out.wav trim 0:00:00.5 0:00:05.5 # grab a 5 second sample sox in.wav out.wav reverse # reverse the file sox bo_diddley.wav bo_short.wav trim 0:00:01.5 0:00:08.5 Enlightened Sound Daemon - esd, esdcat, esdfilt, esdplay, esdsample esdplay bo_short.wav # plays WAV files and many other formats