CSE 303: Autumn 2006, Assignment 1
Due: Friday 6 October 2006, 2:00pm
This is an individual assignment in which you will get experience using the Linux bash shell, using emacs, and writing a short script.
1. Exploring Commands
First run the command script
problem1.txt from the bash prompt. Now try running some commands. The script program you just ran will record what you
try. Run at least 75 different commands subject to the following
When you are finished, type exit.
- Only commands that succeed (do not print an error) count.
- For this problem, two commands are different if they use different programs and/or different
options, but not just different filenames. (Examples: ls and ls -a are different but ls foo and ls bar are the same.)
2. Exploring Command-line Editing
Suppose you mean to type cat foo, but you accidentally type cta
foo. You notice this when the cursor is to the right of the second “o” (you haven’t hit return yet).
- Describe keystroke-by-keystroke how to turn the command-line
into cat foo without typing any letters -
that is, only using control/meta key combinations, arrow keys and
other non-alphabetic keys. Give at least two solutions.
- Suppose your previous command was cat food. Give a third solution that uses this useful fact.
Write your solution in a file called problem2.txt.
3. Commands and Output
Use each of the following commands such that hello (and nothing more) is printed (on standard out, and nothing is printed on standard error). You can
precede your commands with other commands (e.g., to create a file) and/or pass options to your
Hint: The last one is tricky. Put a script that does nothing in a file with a particular name.
In a text file called problem3.txt, describe your solution, including each command you use and a very brief
explanation of it.
- Using an alias, make it so that cta works just like cat. For example cta foo would print the
contents of file foo.
- Make an alias lsdd (stands for “list double-dots”) such that typing lsdd at the prompt does the
Hint: Use two options (see man ls) and two file-patterns (one for hidden files and one for others).
- It prints every file or directory in the current directory that has two or more dots in its name.
If a file starts with "." and has one or more other dots, it should be printed.
- It prints only one file name per line and only the files' names.
- For directories, it prints the directory name, not its contents.
- Note that the “parent directory” (..) will always be printed.
Put your solution in a file called myaliases. Running source myaliases should successfully add the
aliases to the shell.
5. Short Script
Write a bash script in a file mycat that takes 3 or more arguments (let’s call them f1, f2, f3, ..., fn) and works as follows:
- It treats all arguments as filenames.
- If fewer than three arguments are given, it prints an appropriate error message on stderr and exits.
- If a file (or directory) named f1 or f2 exists, it prints “Error: file exists” on stderr and exits.
- Else it concatenates the contents of f3, ..., fn into file f1. It should not print any error messages from this (for example, if some file does not exist or is a directory). Instead, such error messages should be saved in file f2.
- Note: Put filenames in double-quotes in case they contain “funny characters”.
This problem can be done in 15 lines. Longer is fine within reason.
Hints: shift, $@, -lt, -a
6. Extra Credit
Note: Remember the course policy on extra credit. You should not expect solving this problem to have a large effect on your grade. It's here for those of you who like a challenge.
- Make an alias lsdd2 that is like lsdd except that it does not print the parent-directory (..).
Hint: Pipe to grep.
- Make a script mycat2 that is like mycat except it detects duplicates, i.e., it puts the contents of a
file in the output at most once. Hint: Use loops to output one file at a time (using >> to append)
and an array to store what files have already been output.
Your solutions should be
- Correct shell scripts that run on attu.cs.washington.edu
- In good style, including indentation and line breaks
- Of reasonable size
Use the standard turn-in instructiond described here. Your directory should contain the files problem1.txt, problem2.txt, problem3.txt, myaliases, and mycat. If you do the extra credit, put lsdd2 in myaliases and also turn in mycat2.