Introduction to UNIX
Shell Script Exercises


Simple Script

  1. Open a new window or use an existing window. Change directory to the Scripts exercise directory:
    
         cd ~/Scripts
         

  2. Edit a new file - call it simple. Include the following lines in your simple file:
    
         #!/bin/csh
         #
         #Simple Script
         #
         echo Welcome to the world of script files
         echo -n "The current time and date are: "
         date
         
    Save your file and exit the editor.

  3. Make this file world executable. Check its permissions to make sure:
    
         chmod a+x simple
         ls -l simple
         

  4. Execute the file by invoking its name: simple. Observe the output. How does the -n option modify the output from echo?

  5. Return to the tutorial to learn about the next section before proceeding.

Expressions

  1. Display the file simple.2 which is already provided for you in your ~/Scripts directory. Note the use of a couple simple expressions.

  2. Check to make sure the file is executable. If it isn't, then use the chmod to make it executable at least by you.
    
         ls -l simple.2
         chmod u+x 
         

  3. Execute the example file by invoking its name: simple.2 and observe the output.

  4. Now, edit the file and add the lines below. When finished, save the file and try executing it again.
    
         @ count = 1
         echo Count begins as $count
         @ count = $count + 100
         echo Count plus 100 = $count
         
  5. Return to the tutorial to learn about the next section before proceeding.

if

  1. Create a new file called simple.3 which include these lines:
    
         #!/bin/csh
         if ($#argv == 0) echo There are no arguments
         if ($#argv != 0) echo There are $#argv arguments
         

  2. After saving the file, make it executable and then run it as shown:
    
         chmod +x simple.3
         simple.3
         

  3. Now try running it several more times, but with a different number of arguments each time. For example:
    
         simple.3  one two
         simple.3  a b c 3 4
         simple.3  9*3 65 100.34    - will cause an error - why? 
         simple.3  '9*3' 65 100.34  
         simple.3  '10 disc write'  - why is this considered 1? 
         

  4. Edit the file again, adding the following lines:
    
         if (-e $1) echo The file: $1 exists.
         if (! -e $1) echo The file: $1 does not exist.
         

  5. Now execute the file with a filename as the only argument and observe the output:
    
         simple.3  nosuchfile
         simple.3  simple.3
         simple.3  simple
         

  6. Return to the tutorial to learn about the next section before proceeding.

if then else

  1. Display the file simple.4 which is already provided for you in your ~/Scripts directory. It is the same as the one from the tutorial. Attempt to understand what the expressions are evaluating and the logic of the if-then-else nesting.

  2. Make sure the file is executable (you know how to do this by now) and then try running it with different arguments. Compare the output with the script's logic as you understand it. Do they agree?
    
         simple.4 34
         simple.4 199
         simple.4 1000
         simple.4 -33
         simple.4 
         

  3. Return to the tutorial to learn about the next section before proceeding.

foreach

  1. Display the file simple.5 which is already provided for you in your ~/Scripts directory. Understand what it does and its use of the foreach construct.

  2. Make sure the file is executable (you know how to do this by now) and execute it. Does it correctly determine your login machine?

while

  1. Now create your own executable script by copying the while loop example from the tutorial. It is reproduced below for convenience:
    
         #!/bin/csh
         set word = "anything"
         while ($word != "")
            echo -n "Enter a word to check (Return to exit): "
            set word = $<
            if ($word != "") grep $word /usr/share/dict/words
         end
         
  2. Give it execute permission and then run it. What does it do?

  3. Return to the tutorial to learn about the next section before proceeding.

switch / case / breaksw / endsw

  1. Display the file simple.6 which is already provided for you in your ~/Scripts directory. This file is a little lengthier than previous files. Notice the use of the switch construct.

  2. Make sure the file is executable (you know how to do this by now) and execute it.

  3. Now edit the simple.6 file and add the necessary commands to handle the month of Dec. Re-execute the script. Does it still work?

  4. Return to the tutorial to learn about the next section before proceeding.