Shell Skills
Home Up Stream A Stream B Shell Skills RegExp Skills Perl Skills Resources Using FTP Web Logs Permissions Notes 1 Notes 2 Inodes Unix exploit - PATH

 

Shell Scripting Skill Assessment

Here are some descriptions of programming problems of varying levels of difficulty.   Ths skills to do these problems come from Chapter 10 in your Unix text.

To succeed in becoming a Web Programmer, you must be able to do all the scripts at the level of the Elementary programming problems given here.  You must be able to do most of the Basic scripts.

Elementary

Loops or conditional branching statements; but, not used together.

  1. Echo "odd" or "even" based on the number of arguments to the script.
  2. Generate the numbers from 1 to 10.  Now, print them along with their squares.
  3. Generate into a file the numbers ("x") from -10 to 10 each paired with the value of y = x ** 2 + 2x + 10.  (The notation "x ** 2" means "x squared".)  Now add a third column for y = x ** 3.
  4. Count down (print the numbers) from the number of arguments on the command line to zero.
  5. Echo on a separate line each argument given on the command line.
  6. Check the first argument passed to the script and print whether it is a file or a directory.
  7. Compare the first three arguments as text strings and print which ones differ.
  8. Print the name and first and last line of every file in the current directory.
  9. Rename all the pathnames given on the command line to have a ".bak" extension.
  10. Copy standard input to standard output, adding line numbers.
  11. Convert text month names to numeric month numbers, e.g. "Jan" to "1".
  12. Convert numeric months to text months, e.g. "1" to "Jan".

Basic

Combining loops and conditional statements.

  1. Count the number of files and the number of directories given on the command line.
  2. Print the type (file or directory), or size, or owner, or any other attribute, of each of the command line arguments.
  3. Print the names and sizes of all pathnames given on the command line that are files and that exceed a given size.
  4. For all pathnames given on the command line, make sure that if the pathname is an executable file the file must start with "#!/bin/sh", or if the file starts with "#!/bin/sh" the file must be an executable file.  Print a message if neither statement is true.
  5. Compare all the arguments given on the command line and print a message if they are not all the same.  Print the first one that is different.
  6. Check to see that each argument given on the command line is a valid userid (i.e. the user could log in to this machine).
  7. Accumulate and print a running total of the sizes of all files given as command line arguments.
  8. Keep appending the first argument given to the script into a file until the file size is bigger (in bytes) than the second argument to the script.
  9. Rename all the pathnames given on the command line to have a ".bak" extension; but, only if the given pathname does not already have a ".bak" extension and only if a file with that name ending in ".bak" does not already exist.  (Do not create a ".bak.bak" file and do not overwite any existing file.)

Advanced

Sophisticated use of loops and conditional statements.

  1. Print a calendar of dates for the current year (perhaps disregarding leap years).  (Your script output might be a simpler version of the output of the Unix "cal" command.)
  2. Create an ASCII graph of file sizes for pathnames given on the command line.  The graph must auto-size its output so that the biggest file always produces the longest line, but the line does not go past the right edge of the screen.  The output might look like this:
    $ ./script /bin/*
    Biggest file is 527 blocks
    /bin/arch 3
    /bin/bash 407 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    /bin/bash1 333 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    /bin/cat 9
    /bin/chgrp 10 x
    /bin/chmod 10 x
    /bin/chown 10 x
    /bin/compress 0
    /bin/cp 23 xx
    /bin/csh 0
    /bin/date 34 xxx
    /bin/dd 14 x
    /bin/df 15 x
    ...etc... 
  3. Modify the above graph output to pad all file names with blanks on the right to be as wide as the widest file name and all numbers with blanks on the left to be as wide as the widest number, e.g.
    $ ./script /bin/*
    Biggest file is 527 blocks
    /bin/arch       3
    /bin/bash     407 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    /bin/bash1    333 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    /bin/cat        9
    /bin/chgrp     10 x
    /bin/chmod     10 x
    /bin/chown     10 x
    /bin/compress   0
    /bin/cp        23 xx
    /bin/csh        0
    /bin/date      34 xxx
    /bin/dd        14 x
    /bin/df        15 x
    ...etc... 

    Optional: Truncate file names longer than, say, 20 characters.  If you truncate a file name, put a "+" in the last position of the name to indicate the truncation, e.g. "/home/alleni/verylongfilena+". 
    (Hint: awk can print substrings: echo "1234567890" | awk '{print substr($1,0,5)}' will print five characters ("12345") from the first input field.)

 

Additional Resources

See the entries for Shell Scripting and Basic Programming in the FastTrack Resources page.