Difference between revisions of "CSC220 Lab 2 2010"
(→Version 2) |
(→Version 2) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 303: | Line 303: | ||
==Collage== | ==Collage== | ||
+ | |||
+ | * Note: the two versions below should remove the extra files that they generate... They are not ''clean'' in the sense that they leave intermediate files in the working directory... | ||
+ | |||
===Version 1=== | ===Version 1=== | ||
<code><pre> | <code><pre> | ||
Line 406: | Line 409: | ||
</pre></code> | </pre></code> | ||
− | |||
<br /> | <br /> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | [[Category:CSC220]][[Category:Labs]] | + | [[Category:CSC220]][[Category:Labs]][[Category:Bash]] |
Latest revision as of 17:25, 17 November 2010
Contents
Setup your account
Wait until I have figured out why this modification affects the login before doing this section...
- Sharon's login modification: edit the file .login in your 220a-xx account, and add this line at the end of it:
if ( -f /bin/bash ) exec /bin/bash --login
- edit the file .bash_profile in your 220a-xx account and add the following lines at the end of it:
export SHELL=/bin/bash unalias rm unalias cp unalias mv
- Logout, and log back in. You should automatically be in bash, and the rm, cp, and mv commands should not be aliased (more about this in class).
Generating New Accounts
- At the beginning of the semester, our Linux administrators must create new accounts for the classes. The accounts for our class are 220a-aa, 220a-ab, ... 220a-az.
- Write a bash for-loop that will display the all the accounts for CSC220:
220a-aa 220a-ab ... 220a-az
- Modify your for-loop so that it prints a string that could be the command for creating a new account. Something like:
adduser 220a-aa adduser 220a-ab ... adduser 220a-az
- Same question, but now accounts must be generated for CSC111, CSC220, CSC231, CSC240, and CSC353. Write the bash command or combination of commands that will print all the accounts, from a to z, for each of the classes.
Pinging computers
- Ping is a utility that sends a packet to a server with a known address or IP, and checks if the message comes back.
- Try it out:
ping -c 1 grendel.csc.smith.edu PING grendel.csc.smith.edu (131.229.72.9) 56(84) bytes of data. 64 bytes from grendel.csc.smith.edu (131.229.72.9): icmp_seq=1 ttl=64 time=0.340 ms --- grendel.csc.smith.edu ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 1ms rtt min/avg/max/mdev = 0.340/0.340/0.340/0.000 ms
- The -c 1 switch specifies that only one packet should be sent.
- Notice that if grendel is up, we will get a packet back, which is indicated by the "1 received" string in the output.
- See what happens if the computer is not responding, or if there is no computer at the given IP:
ping -qc 1 131.229.255.255 PING 131.229.255.255 (131.229.255.255) 56(84) bytes of data. --- 131.229.255.255 ping statistics --- 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 7ms
- Get a copy of the file hadoop.ips as follows:
getcopy hadoop.ips
- Take a look at the contents of the file. It contains the IP addresses of all the Hadoop machines in FH342.
- Try this command out, at the prompt:
for ip in `cat hadoop.ips` ; do echo $ip done
- Observe the output...
- Modify the loop so that it outputs the IP of computers that are currently running and responding to ping commands. Figure out a way to display only lines that contain IPs, and not lines describing the number of packets sent or received...
- Modify the loop so that it outputs the IP of computers that are not responding to pings.
Creating a Collage of Images
Important Note: this lab should be run in Linux mode with X11 support. If you are doing this lab in FH342, make sure the machine you are using booted with Fedora, and you should be all set. If you are using a Linux box or a Mac in another location, make sure you ssh to beowulf or grendel using the -Y option:
ssh -Y 220a-xx@beowulf.csc.smith.edu
The -Y option allows commands that generate graphics to open windows on your Linux box.
- This part of the lab uses a very nice and powerful program that is often bundled in the various Linux distribution (Fedora, Ubuntu, Suse, etc): ImageMagick. It is a collection of different programs that allow for very clever and powerful image manipulations.
Part 1
- There are 3 images in the 220a public_html folder, called 220a.jpg, 220b.jpg, and 220c.jpg. You can see them by opening this URL: http://cs.smith.edu/~220a/
- use wget (see Lab 1) to retrieve all three images into your account. Use a for-loop! Something like this is a good start:
for i in a b c ; do echo $i echo 220${i} done
- verify that you got the images by displaying them with the display command (also part of imagemagick). Example:
display 220a.jpg
- Can you tell what these three pictures have in common?
- You are going to use the Overlay feature of ImageMagick, covered here: http://www.imagemagick.org/Usage/annotating/#overlay
- Overlay the 220 road sign image on top of the car:
composite -geometry +10+10 220a.jpg 220c.jpg 220ac.jpg
- Display 220ac.jpg to see the result. The +10+10 part of the command specify that the top-left corner of the first image (road sign) should be 10 pixels left and 10 pixels down from the second image (car). The result is a new file called 220ac.jpg.
- Using the same approach overlay image 220b.jpg on 220ac.jpg, and offset it by 850 pixels left, and 10 down (or pick your own coordinates; this is not important for this lab). Call the resulting file 220abc.jpg.
- Add the caption "CSC 220" (or whatever text you want) to the bottom of the resulting image as follows:
convert 220abc.jpg -pointsize 20 \ -draw "gravity south fill white text 0,10 'CSC 220' " \ 220abcfinal.jpg
- Check that you can see the white text at the bottom of the 220abcfinal.jpg image.
- Store the resulting file in your public_html directory, and make it readable by all:
cp 220abcfinal.jpg ~/public_html chmod a+r ~/public_html/*
- Verify that you can access your picture on the Web: http://cs.smith.edu/~220a-xx/220abcfinal.jpg (make sure you replace xx by your 2-letter Id!)
Part 2
- Create a script file that will do all the work that you did by hand in the previous section.
- Call your script collage.sh (for example), and make sure its first line is
#! /bin/bash
- Also make sure you make your script executable when you're done:
chmod +x collage.sh
- remove the 220abcfinal.jpg file from your public_html directory
rm ~/public_html/220abc*.jpg
- verify that if you reload your browser, the image is not available any longer.
- Run your script.
./collage.sh
- Reload your browser. Does the image reappear? If yes, congrats! If not, figure out why!
Part 3
- modify your script so that it performs the same series of actions on three other files in the ~220a public_html directory:
- smith_a.jpg
- smith_b.jpg
- smith_c.jpg
- you should get a smith_abcfinal.jpg image in your account's public_html directory.
- verify that you can access it with your broser.
Part 4
- modify your script some more so that you can make it work with either the smith_x.jpg files or the 220x.jpg files.
In one case you would start your script this way:
collage.sh smith_
- In the other case,
collage.sh 220
- The script will assume that the extension will always be .jpg...
Homework Assignment
You are now ready for Homework #1!
Selected Solutions
Adduser
Option 1
#! /bin/bash
# lab2_2.sh
# D. Thiebaut
# prints the string adduser xxx
# where xxx represents all the user names
# for the CSC classes
#
# Usage:
# lab2_2.sh
#
for user in `ls /Users/classes/ | grep -v test | grep -v OLD | grep -v acct` ; do
for letter in {a..z} ; do
echo "adduser ${user}-a${letter}"
done
done
Option 2
#! /bin/bash
# lab2_2.sh
# D. Thiebaut
# prints the string adduser xxx
# where xxx represents all the user names
# for the CSC classes
#
# Usage:
# lab2_2.sh
#
for user in 100 111 212 231 240 290 352 ; do
for letter in a b c d e f g h i j k l m n o p q r s t u v w x y z ; do
for semester in a b ; do
echo "adduser ${user}${semester}-a${letter}"
done
done
done
Pinging Computers
Alive
#! /bin/bash
# alive.sh
# D. Thiebaut
#
# pings the PCs in FH342 and reports the IP of the ones responding
# Requires the file hadoop.ips that contains the IPs of all the
# computers.
#
# Usage:
# alive.sh
#
# the \ symbol at the end of a line indicates that the command
# is not finished and continues on the next line... (A nice way
# to organize commands in a nice way.)
for ip in `cat hadoop.ips` ; do
ping -c 1 $ip | grep -B 1 "1 received" \
| grep ping \
| cut -d' ' -f 2
done
Dead
#! /bin/bash
# dead.sh
# D. Thiebaut
#
# pings the PCs in FH342 and reports the IP of the ones that do
# not respond.
# Requires the file hadoop.ips that contains the IPs of all the
# computers.
#
# Usage:
# dead.sh
#
# the \ symbol at the end of a line indicates that the command
# is not finished and continues on the next line... (A nice way
# to organize commands in a nice way.)
for ip in `cat hadoop.ips` ; do
ping -c 1 $ip | grep -B 1 "0 received" \
| grep ping \
| cut -d' ' -f 2
done
Collage
- Note: the two versions below should remove the extra files that they generate... They are not clean in the sense that they leave intermediate files in the working directory...
Version 1
#! /bin/bash
# collage.sh
# D. Thiebaut
#
# Solution program for Lab 2
#
URL=http://cs.smith.edu/~220a
#--- fetch the 3 images ---
for id in a b c ; do
wget ${URL}/220${id}.jpg
echo "fetched 220${id}.jpg"
done
#--- overlay 220a over 220c and create 220ac.jpg ---
composite -geometry +10+10 220a.jpg 220c.jpg 220ac.jpg
#--- overlay 220b over 220ac and get 220abc.jpg ---
composite -geometry +850+10 220b.jpg 220ac.jpg 220abc.jpg
#--- add caption and create 220abcfinal.jpg ---
convert 220abc.jpg -pointsize 20 \
-draw "gravity south fill white text 0,10 'CSC 220' " \
220abcfinal.jpg
#--- copy to public_html, and make readable by all ---
cp 220abcfinal.jpg ~/public_html/
chmod a+r ~/public_html/*
#--- tell the user to point his/her browser to the file just created---
for i in {1..10}; do echo "" ; done
echo "Point your browser to ${URL}/220abcfinal.jpg and see the result\!\!\!"
for i in {1..10}; do echo "" ; done
Version 2
- This version allows the user to specify the prefix for the file, for example 220 or smith_.
#! /bin/bash
# collage2.sh
# D. Thiebaut
#
# Solution program for Lab 2
#
# Usage:
# collage2.sh file_prefix
#
# Example:
# collage2.sh 220a
# collage2.sh smith_
#
# Debugging: uncomment next line to see what the script actually does when it runs...
# set -x
if [ "$#" -eq "0" ]
then
echo "Syntax: collage2.sh prefix"
echo ""
echo "will create a collage of pictures all called prefixa.jpg,"
echo "prefixb.jpg and prefixc.jpg, and will store the resulting"
echo "collage in the public_html directory, with name"
echo "prefixabcfinal.jpg, where prefix is the paramter following"
echo "the program name on the command line."
exit 1
fi
URL=http://cs.smith.edu/~220a
prefix=$1 # first parameter on command line
#--- fetch the 3 images ---
for id in a b c ; do
wget ${URL}/${prefix}${id}.jpg
done
#--- overlay 220a over 220c and create 220ac.jpg ---
composite -geometry +10+10 ${prefix}a.jpg ${prefix}c.jpg ${prefix}ac.jpg
#--- overlay 220b over 220ac and get 220abc.jpg ---
composite -geometry +850+10 ${prefix}b.jpg ${prefix}ac.jpg ${prefix}abc.jpg
#--- add caption and create 220abcfinal.jpg ---
convert ${prefix}abc.jpg -pointsize 20 \
-draw "gravity south fill white text 0,10 '${prefix}' " \
${prefix}abcfinal.jpg
#--- copy to public_html, and make readable by all ---
cp ${prefix}abcfinal.jpg ~/public_html/
chmod a+r ~/public_html/*
#--- tell the user to point his/her browser to the file just created---
for i in {1..10}; do echo "" ; done
echo "Point your browser to ${URL}/${prefix}abcfinal.jpg and see the result\!\!\!"
for i in {1..10}; do echo "" ; done