Syllabus 20091027
Some examples: "jpegloop" and "foodphotos"
Some exercises to round off our BASH experiences:
- Pimp your prompt
- Custom commands with shell scripts
- Return to "Command-line After Effects" aka Parameterized Movie
Customizing your shell prompt
(Handout: Prompt string customizations)
Making your own custom scripts
1. Save the file in your "bin" folder (or somewhere in your $PATH). You don't need to use the ".sh" extension (if you want a "cleaner" command name). For example, you might call it "myscript". 2. Add the "sh-bang" as the FIRST line in the file:
#!/bin/bash
3. From the terminal:
chmod +x myscript
Positional parameters
Exercise: Cut out a circular shape from an image There isn't a single command to do this in imagemagick (the built in crop command only works for rectangles) but it's possible to combine multiple steps to produce a circular cut-out.
Command Substitution
Command substitution allows you to have the bash do something, then paste the result into your script to do something else with.
$( command here )
A useful example is using imagemagick's identify command to get the width or height of an image.
identify -format %w myimage.png
width=$(identify -format %w myimage.png)
echo Your image is $width pixels wide.
Example: Make an image labeller, using image magick
#!/bin/bash
in=$1
out=$2
text=$3
width=$(identify -format %w $in)
convert -background '#0008' -fill white -gravity SouthWest -size ${width}x36 \
-font /home/murtaugh/.fonts/NotCourier-sans-Bold.ttf -pointsize 36 \
caption:"$text" +size $in +swap -gravity south -composite $out
Looping
Counting to 10
for ((i=1;i<=10;i++))
do
echo i say $i
done
Now with play...
for ((i=1;i<=10;i++))
do
play -c1 -r8000 -n synth 0.25 sine 220 vol 0.7
done
Sequential filenames, using command-substitution and the printf command:
f=$(printf %04d.wav $i)
mkdir out
for ((i=1;i<=10;i++))
do
f=out/$(printf %04d $i)
sox -c1 -r44100 -n $f.wav synth 0.25 sine %$i vol 0.7
done
# combine all the individual files to a single file
sox out/*.wav audio.wav
Example with imagemagick:
mkdir out
for (( i=1; i<=100; i++ ))
do
imagefile=$(printf %04d $i)
echo i is $i, imagefile is $imagefile
convert frame.png -blur ${i}x${i} out/$imagefile.png
done
ffmpeg -i out/%04d.png -y -sameq blurry.mpeg
Combining audio & video into a single movie.
ffmpeg -i audio.wav -i video.mpeg final.mpeg
Or using OGG as the video format:
ffmpeg -i audio.wav -i video.ogv final.ogv
On to Python
Central themes: How does the computer deal with different "types" of data. What are "ints", "floats", and "strings" really? What is an encoding? How do numbers, text, image, sound all end up as "one's and zero's" of binary?
Reading for next week: ThinkPython Chapter 2 'plus' additional exercises
Values & Types, Variables, Keywords, Statements, Operators, Expressions, Order of operation, Comments
Linear Mapping: evt. examples in JavaScript (to match their AJAX experiments?)