NEW: For a prettier blog interface, see the Wordpress version!

Instructions

Work on the following exercises individually. You are allowed to consult general shell scripting references such as the Advanced Bash Scripting Guide and the Bash Programming Introduction HOWTO (both at http://www.tldp.org) and search the Internet, but try as much as possible to solve the exercises on your own, referring to the man pages, apropos database, and slides as needed.

Don't worry about getting stuck. Try as hard as you can to finish the exercises on your own. If you find yourself at a dead end, write down everything you tried to do and e-mail me. If I don't reply within the weekend, at least you'll have questions for class on Wednesday.

E-mail me a tar.gz containing all your shell scripts and whatever notes you might have by 11:59:59 PM on Tuesday, December 9, 2003. Please feel free to e-mail me before then. (In fact, you are strongly encouraged to do so.)

Exercises

hello-world.sh: echo

Write a shell script that, when invoked with ./hello-world.sh, prints out

Hello, world!
- your name here <your e-mail address>
(Substitute your name.)

hello-person.sh: arguments, echo

Write a shell script that, when invoked with ./hello-person.sh name, prints out

Hello, name!
- your name here <your e-mail address>
Example: ./hello-person.sh Sacha by Foo Bar <[email protected]> should print out

Hello, Sacha!
- Foo Bar <[email protected]>

hello-people.sh: loops, arguments, echo

Write a shell script that, when invoked with ./hello-people.sh person1 person2 person3, prints out

Hello, person1!
Hello, person2!
Hello, person3!
- your name here <your e-mail address>
Your program should work for any number of people, not just three. You will need to learn about loops.

greet.sh: if, test, date, cut, command substitution, echo

Write a shell script that greets you "Good morning", "Good afternoon", or "Good evening" depending on the time.

greet-english.sh and greet-filipino.sh: arg0, if, test, date, cut, command substitution, echo

Write a shell script that changes its behavior depending on how it's called. greet-english.sh and greet-filipino.sh should have exactly the same source code, and should take no parameters or use external variables - just use $0. For greet-filipino.sh, choose between "Magandang umaga", "Magandang hapon", and "Magandang gabi" depending on the time.

missing.sh: if, test, loops, arguments, echo

Write a shell script that checks for missing files. For example, if ls reports the following files foo baz and you call your script with ./missing.sh foo bar baz qux, the script should print out

bar
qux
because those two files can't be found in the current directory.

package-scripts.sh: tar, gzip, date, command substitution, wildcards

To make it easier for you to submit your work, write a shell script that creates a tar.gz of all the files ending in '.sh' in the current directory. The filename of the resulting archive should be of the form "studentid-date-time.tar.gz". Example: 990577-2003.12.07-23.59.tar.gz . This shell script takes no arguments.

contact.sh: grep, cut, if, arguments, echo, etc.

Summary

./contact.sh add "First Name" "Last Name" email phone
Add the record to contacts.txt, creating it if necessary. See format of contact.txt for details.

./contact.sh remove data
Remove the record from contacts.txt

./contact.sh search data
Print out the records that match data, alphabetically sorted (last name, then first name). See output format for details.

Format of contact.txt

contact.txt should contain lines of the form

Last Name:First Name:e-mail:phone

Output format

Name:   First name Last Name
E-mail: email
Phone:  phone
A blank line separates entries.
Previous day | Next day

I'd love to hear about any questions, comments, suggestions or links that you might have. Your comments will not be posted on this website immediately, but will be e-mailed to me first. You can use this form to get in touch with me, or e-mail me at [email protected] .

Page: C S 161.Shell Scripting.Exercise 1
Updated: 2004-11-21
NOTE: ANTI-SPAM MEASURE NOW IN PLACE. Please answer the following question with the right number in order to send me your comment.
What is two minus one? (hint: one ;) )
Name:
E-mail:
URL:
Comments: