Scripting class, answers 1

1. In firefox, right click on an image and do Copy link location. Then use that link in your script. {1..21} is shorthand for the space separated list from 1 to 21, i.e.: “1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21”.

[ $ii -le 21 ] is shorthand for “test $ii -le 21”

# using a for loop
for ii in {1..21}
do
        wget "http://helms-deep.net/~rwh/blog/wp-content/uploads/2011/03/img${ii}.jpg"
done
 
# using a while loop
ii=1
while [ $ii -le 21 ] # spaces are required
do
        wget "http://helms-deep.net/~rwh/blog/wp-content/uploads/2011/03/img${ii}.jpg"
        ii=`expr $ii + 1` # spaces are required
done

For an interesting discussion: why did I use ii for my incrementer instead of i?

2. Note the double quotes, which are required to escape the filenames which contain special characters (spaces and parentheses). It is always good practice to wrap variables that contain filenames in double quotes for this reason.

for file in img*.jpg
do
        mv "$file" "`basename "$file" .jpg` (modified).jpg"
done

3. First, make sure imagemagick is installed:

sudo apt-get install imagemagick
for file in img*\ \(modified\).jpg 
do 
        convert -resize 50% "$file" "`basename "$file" .jpg` (small).jpg" 
done

4. First, we install the software we need:

sudo apt-get install vorbis-tools
sudo apt-get install lame

Then we convert the files:

wget http://helms-deep.net/~rwh/files/lara_st_john.tar.gz
tar xvzf lara_st_john.tar.gz
cd lara_st_john/bach_violin_concertos
for file in *.ogg
do
        oggdec "$file"
        lame --vbr-new `basename "$file" .ogg`.wav `basename "$file" .ogg`.mp3
done

5. First, we extract the track information from the ogg file using the vorbiscomment command and save it in a temporary file. Then we use grep and sed to select each datum and save it in a shell variable. Once we have all this info ready, we can decode the ogg file to wav, then encode the wav to mp3, passing in the shell variables which contain the track information.

for file in *.ogg
do
        base=`basename "$file" .ogg`
        vorbiscomment "$file" > "$base.comment"
        tt=`grep -e "^title"  "$base.comment" | sed -e 's/title=\(.*\)$/\1/'`
        ta=`grep -e "^artist" "$base.comment" | sed -e 's/artist=\(.*\)$/\1/'`
        tg=`grep -e "^genre"  "$base.comment" | sed -e 's/genre=\(.*\)$/\1/'`
        ty=`grep -e "^date"   "$base.comment" | sed -e 's/date=\(.*\)$/\1/'`
        tl=`grep -e "^album"  "$base.comment" | sed -e 's/album=\(.*\)$/\1/'`
        oggdec "$file"
        lame --vbr-new --tt "$tt" --ta "$ta" --tg "$tg" --ty "$ty" --tl "$tl" "$base.wav" "$base.mp3"
        rm -f "$base.comment"
        rm -f "$base.wav"
done
This entry was posted in answers, scripting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *