Link to
Colorado State University Home Page

Assignment 4: Unix and Shell Scripts

Description

You must write a shell script named assign4 that, when executed, builds the directory tree and files shown below. This diagram shows the required directory structure and files within those directories. Most files are text files. One is a Java file and its compiled class file.

Your shell script will at least include the commands mkdir, cd, cat, chmod, javac, and java. Use chmod to set permissions that prevent anyone but you from listing the contents of directories and from viewing any files. Use an editor like pico or emacs to create your script file.

The last command of your shell script must be the dtree command so that just before your shell script completes, a character-representation of the directory tree and files will appear. You may download the dtree script file by clicking on this link or by clicking right on the link and selecting "Save link as ...". You may read an explanation of this script here. Don't forget to make dtree an executable file!

Using cat to Create Files

Your script must create several text files, using the Unix cat command. cat can be used to quickly create a file from the command line by doing:
> cat > newfile
first line
second line
(control-d)
Now you can disply the contents of the file by again using cat:
> cat newfile
first line
second line

However, to create a file from a script file you cannot type the control-d character to terminate the input. Instead, you must use what is referred to as a here-document. The relevant information from the man page for bash is:

 The format of here-documents is as follows:

          <<[-]word
                  here-document
          delimiter

     No  parameter  expansion,  command  substitution,   pathname
     expansion, or arithmetic expansion is performed on word.  If
     any characters in word are  quoted,  the  delimiter  is  the
     result  of quote removal on word, and the lines in the here-
     document are not expanded.  If word is unquoted,  all  lines
     of  the  here-document are subjected to parameter expansion,
     command substitution,  and  arithmetic  expansion.   In  the
     latter  case,  the pair \<newline> is ignored, and \ must be
     used to quote the characters \, $, and `.

     If the redirection operator is <<-,  then  all  leading  tab
     characters  are  stripped from input lines and the line con-
     taining delimiter.  This allows here-documents within  shell
     scripts to be indented in a natural fashion.

Here is an example. If my script file contains

cat <<END > newfile
first line
second line
END
then when the cat command is run, it accepts its input from the following lines until the word END is encountered. So END plays the same role that control-d when we ran cat interactively, from the command line.

Bash

I suggest you set your default shell to bash, because bash is the only shell we have that has a built-in help command, which produces more concise explanations than does the common Unix man command. Remember, you can switch to running bash anytime by just typing the Unix command bash. To change your default shell to bash, you must send e-mail to sna@cs.colostate.edu requesting that your default shell be set to bash.

I have added some links in our On-Line Resources section of the class web page to on-line sources of help with bash.

Once you are running bash, typing the command help gives you this list of shell commands for which you can get more information:

GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)
These shell commands are defined internally.  Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.

A star (*) next to a name means that the command is disabled.

 %[DIGITS | WORD] [&]               . filename
 :                                  [ arg... ]
 alias [-p] [name[=value] ... ]     bg [job_spec]
 bind [-lpvsPVS] [-m keymap] [-f fi break [n]
 builtin [shell-builtin [arg ...]]  case WORD in [PATTERN [| PATTERN].
 cd [-PL] [dir]                     command [-pVv] command [arg ...]
 continue [n]                       declare [-afFrxi] [-p] name[=value
 dirs [-clpv] [+N] [-N]             disown [-h] [-ar] [jobspec ...]
 echo [-neE] [arg ...]              enable [-pnds] [-a] [-f filename] 
 eval [arg ...]                     exec [-cl] [-a name] file [redirec
 exit [n]                           export [-nf] [name ...] or export 
 false                              fc [-e ename] [-nlr] [first] [last
 fg [job_spec]                      for NAME [in WORDS ... ;] do COMMA
 function NAME { COMMANDS ; } or NA getopts optstring name [arg]
 hash [-r] [-p pathname] [name ...] help [pattern ...]
 history [-c] [n] or history -awrn  if COMMANDS; then COMMANDS; [ elif
 jobs [-lnprs] [jobspec ...] or job kill [-s sigspec | -n signum | -si
 let arg [arg ...]                  local name[=value] ...
 logout                             popd [+N | -N] [-n]
 printf format [arguments]          pushd [dir | +N | -N] [-n]
 pwd [-PL]                          read [-r] [-p prompt] [-a array] [
 readonly [-anf] [name ...] or read return [n]
 select NAME [in WORDS ... ;] do CO set [--abefhkmnptuvxBCHP] [-o opti
 shift [n]                          shopt [-pqsu] [-o long-option] opt
 source filename                    suspend [-f]
 test [expr]                        time [-p] PIPELINE
 times                              trap [arg] [signal_spec ...] or tr
 true                               type [-apt] name [name ...]
 typeset [-afFrxi] [-p] name[=value ulimit [-SHacdflmnpstuv] [limit]
 umask [-p] [-S] [mode]             unalias [-a] [name ...]
 unset [-f] [-v] [name ...]         until COMMANDS; do COMMANDS; done
 variables - Some variable names an wait [n]
 while COMMANDS; do COMMANDS; done  { COMMANDS ; }

The .bashrc File

Whenever a new process starts running bash, a script file named .bashrc in your home directory is run, if found. This is where you can define aliases and add directories to the environmental variable PATH, which tells the Unix system in which directories to search for executables. To run javac and java, your PATH must include /usr/local/java/bin. Here is a .bashrc file you can view and save as .bashrc in your home directory.

When You Get Stuck

Use the bash help messages, results from the man command, the links to Unix tutorials on our class web page, and any other help you can get from the net, to figure out how to complete this assignment. Also, send me e-mail if you get stuck. You will probably be stuck several times in finishing this assignment.

If you have missed some classes, there will be parts of this assignment that you do not know how to do. Do not hesitate to ask me for help. By the end of this assignment, I expect you to know my email address (anderson@cs.colostate.edu)!

How to Turn In Your Solution

When you have completed this assignment, send e-mail to me (anderson@cs.colostate.edu) with your script file as an attachment. In the text of the message, please give me some feedback about which parts of this assignment were the most difficult. Also, please let me know what you learned from this assignment and what additional Unix commands you would like to learn about.

By The Way...

I created the diagram on this page using StarOffice, the free suite of Office tools available from Sun. Versions can be downloaded for Solaris, Linux, and Windows. StarOffice tools are pretty good at importing and exporting Microsoft Office formats. Find the information about how to run StarOffice in the CS Department's on-line software information. Here is a link to dirdiagram.sda, the StarOffice file containing my diagram, before I exported it as a jpeg file.